matplotlib 入门之Usage Guide

matplotlib教程学习笔记

Usage Guide

import matplotlib.pyploy as plt
import numpy as np
parts of figure

Figure: Axes, title, figure legends等的融合体?

fig = plt.figure()  # an empty figure with no axes
fig.suptitle('No axes on this figure')  # Add a title so we know which it is

fig, ax_lst = plt.subplots(2, 2)  # a figure with a 2x2 grid of Axes
image.png

Axes: 通常意义上的可视化数据的图,一个figure可以拥有许多Axes. 难道是,一个figure上可以画不同数据的图,每种图就是一个Axes?

Axis: 大概就是通常意义的坐标轴了吧。

Artist: 基本上我们所见所闻都是Artist,标题,线等等。与Axes绑定的Artist不能共享。

plotting函数的输入

最好的ndarray类型,其他对象如pandas, np.matrix等可能会产生错误。

matplotlib, pyplot, pylab, 三者的联系

x = np.linspace(0, 2, 100)

plt.plot(x, x, label='linear')
plt.plot(x, x**2, label='quadratic')
plt.plot(x, x**3, label='cubic')

plt.xlabel('x label')
plt.ylabel('y label')

plt.title("Simple Plot")

plt.legend() #图例

plt.show()
image.png

第一次使用plt.plot,函数会自动创建figure和axes,之后再使用plt.plot,会重复使用当前的figure和axes,新建的artist也会自动地使用当前地axes。

pylab好像把plt.plot和numpy统一起来了,但是会造成名称的混乱而不推荐使用。

Coding style

pyplot style, 在程序开头应当:

import matplotlib.pyplot as plt
import numpy as np

数据+创建figures+使用对象方法:

x = np.arange(0, 10, 0.2)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()
image.png

当面对不同的数据的时候,往往需要设计一个函数来应对,建议采取下面的设计格式:

def my_plotter(ax, data1, data2, param_dict):
    """
    A helper function to make a graph

    Parameters
    ----------
    ax : Axes
        The axes to draw to

    data1 : array
       The x data

    data2 : array
       The y data

    param_dict : dict
       Dictionary of kwargs to pass to ax.plot

    Returns
    -------
    out : list
        list of artists added
    """
    out = ax.plot(data1, data2, **param_dict)
    return out

# which you would then use as:

data1, data2, data3, data4 = np.random.randn(4, 100)
fig, ax = plt.subplots(1, 1) #一个plot
my_plotter(ax, data1, data2, {'marker': 'x'})
image.png
fig, (ax1, ax2) = plt.subplots(1, 2) #2-plots
my_plotter(ax1, data1, data2, {'marker': 'x'})
my_plotter(ax2, data3, data4, {'marker': 'o'})
image.png

Backends 后端

不太明白。
后面的就不看了。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 艺术家教程 原文:Artist tutorial 译者:飞龙 协议:CC BY-NC-SA 4.0 matplot...
    布客飞龙阅读 3,589评论 0 11
  • 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明。谢谢...
    丷菜菜呀阅读 1,726评论 0 1
  • 这篇笔记主要来自《利用python进行数据分析》的第八章第一节,对matplotlib的基本使用介绍的特别清晰。关...
    ydlstartx阅读 995评论 0 2
  • 从昨晚到今天早上,已经在朋友圈,公众号,虎嗅,微博等渠道都刷到公众号可以修改错别字的消息(全文最多只能修改5个字,...
    szkenchen阅读 611评论 0 0
  • 一个服装店铺的陈列起到了关键性的作用,服装店铺门面是顾客看到的第一个印象,而陈列就是顾客进店看到的另一个门面,很多...
    DL心阅读 275评论 0 1