Pyplot 教程¶

Matplotlib 简介¶

matplotlib 是一个 Python2D 图形包。

在线文档:http://matplotlib.org ,提供了 Examples, FAQ, API, Gallery,其中 Gallery 是很有用的一个部分,因为它提供了各种画图方式的可视化,方便用户根据需求进行选择。

使用 Pyplot¶

导入相关的包:

In [1]:

  1. import numpy as np
  2. import matplotlib.pyplot as plt

matplotlib.pyplot 包含一系列类似 MATLAB 中绘图函数的相关函数。每个 matplotlib.pyplot 中的函数对当前的图像进行一些修改,例如:产生新的图像,在图像中产生新的绘图区域,在绘图区域中画线,给绘图加上标记,等等…… matplotlib.pyplot 会自动记住当前的图像和绘图区域,因此这些函数会直接作用在当前的图像上。

下文中,以 plt 作为 matplotlib.pyplot 的省略。

plt.show() 函数¶

默认情况下,matplotlib.pyplot 不会直接显示图像,只有调用 plt.show() 函数时,图像才会显示出来。

plt.show() 默认是在新窗口打开一幅图像,并且提供了对图像进行操作的按钮。

不过在 ipython 命令行中,我们可以使用 magic 命令将它插入 notebook 中,并且不需要调用 plt.show() 也可以显示:

  • %matplotlib notebook
  • %matplotlib inline
    不过在实际写程序中,我们还是需要调用 plt.show() 函数将图像显示出来。

这里我们使图像输出在 notebook 中:

In [2]:

  1. %matplotlib inline

plt.plot() 函数¶

例子¶

plt.plot() 函数可以用来绘图:

In [3]:

  1. plt.plot([1,2,3,4])
  2. plt.ylabel('some numbers')
  3.  
  4. plt.show()

06.01 Pyplot 教程 - 图1

基本用法¶

plot 函数基本的用法有以下四种:

默认参数

  • plt.plot(x,y)
    指定参数

  • plt.plot(x,y, format_str)
    默认参数,x0~N-1

  • plt.plot(y)
    指定参数,x0~N-1

  • plt.plot(y, format_str)
    因此,在上面的例子中,我们没有给定 x 的值,所以其默认值为 [0,1,2,3]

传入 xy

In [4]:

  1. plt.plot([1,2,3,4], [1,4,9,16])

Out[4]:

  1. [<matplotlib.lines.Line2D at 0xa48a550>]

06.01 Pyplot 教程 - 图2

字符参数¶

MATLAB 中类似,我们还可以用字符来指定绘图的格式:

表示颜色的字符参数有:

字符 颜色
‘b’ 蓝色,blue
‘g’ 绿色,green
‘r’ 红色,red
‘c’ 青色,cyan
‘m’ 品红,magenta
‘y’ 黄色,yellow
‘k’ 黑色,black
‘w’ 白色,white

表示类型的字符参数有:

字符 类型 字符 类型
'-' 实线 '—' 虚线
'-.' 虚点线 ':' 点线
'.' ',' 像素点
'o' 圆点 'v' 下三角点
'^' 上三角点 '<' 左三角点
'>' 右三角点 '1' 下三叉点
'2' 上三叉点 '3' 左三叉点
'4' 右三叉点 's' 正方点
'p' 五角点 '*' 星形点
'h' 六边形点1 'H' 六边形点2
'+' 加号点 'x' 乘号点
'D' 实心菱形点 'd' 瘦菱形点
'_' 横线点

例如我们要画出红色圆点:

In [5]:

  1. plt.plot([1,2,3,4], [1,4,9,16], 'ro')
  2. plt.show()

06.01 Pyplot 教程 - 图3

可以看出,有两个点在图像的边缘,因此,我们需要改变轴的显示范围。

显示范围¶

MATLAB 类似,这里可以使用 axis 函数指定坐标轴显示的范围:

  1. plt.axis([xmin, xmax, ymin, ymax])

In [6]:

  1. plt.plot([1,2,3,4], [1,4,9,16], 'ro')
  2. # 指定 x 轴显示区域为 0-6,y 轴为 0-20
  3. plt.axis([0,6,0,20])
  4. plt.show()

06.01 Pyplot 教程 - 图4

传入 Numpy 数组¶

之前我们传给 plot 的参数都是列表,事实上,向 plot 中传入 numpy 数组是更常用的做法。事实上,如果传入的是列表,matplotlib 会在内部将它转化成数组再进行处理:

In [7]:

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. # evenly sampled time at 200ms intervals
  5. t = np.arange(0., 5., 0.2)
  6.  
  7. # red dashes, blue squares and green triangles
  8. plt.plot(t, t, 'r--',
  9. t, t**2, 'bs',
  10. t, t**3, 'g^')
  11.  
  12. plt.show()

06.01 Pyplot 教程 - 图5

传入多组数据¶

事实上,在上面的例子中,我们不仅仅向 plot 函数传入了数组,还传入了多组 (x,y,format_str) 参数,它们在同一张图上显示。

这意味着我们不需要使用多个 plot 函数来画多组数组,只需要可以将这些组合放到一个 plot 函数中去即可。

线条属性¶

之前提到,我们可以用字符串来控制线条的属性,事实上还可以通过关键词来改变线条的性质,例如 linwidth 可以改变线条的宽度,color 可以改变线条的颜色:

In [8]:

  1. x = np.linspace(-np.pi,np.pi)
  2. y = np.sin(x)
  3.  
  4. plt.plot(x, y, linewidth=2.0, color='r')
  5.  
  6. plt.show()

06.01 Pyplot 教程 - 图6

使用 plt.plot() 的返回值来设置线条属性¶

plot 函数返回一个 Line2D 对象组成的列表,每个对象代表输入的一对组合,例如:

  • line1, line2 为两个 Line2D 对象

line1, line2 = plt.plot(x1, y1, x2, y2)

  • 返回 3 个 Line2D 对象组成的列表

lines = plt.plot(x1, y1, x2, y2, x3, y3)

我们可以使用这个返回值来对线条属性进行设置:

In [9]:

  1. # 加逗号 line 中得到的是 line2D 对象,不加逗号得到的是只有一个 line2D 对象的列表
  2. line, = plt.plot(x, y, 'r-')
  3.  
  4. # 将抗锯齿关闭
  5. line.set_antialiased(False)
  6.  
  7. plt.show()

06.01 Pyplot 教程 - 图7

plt.setp() 修改线条性质¶

更方便的做法是使用 pltsetp 函数:

In [10]:

  1. lines = plt.plot(x, y)
  2.  
  3. # 使用键值对
  4. plt.setp(lines, color='r', linewidth=2.0)
  5.  
  6. # 或者使用 MATLAB 风格的字符串对
  7. plt.setp(lines, 'color', 'r', 'linewidth', 2.0)
  8.  
  9. plt.show()

06.01 Pyplot 教程 - 图8

可以设置的属性有很多,可以使用 plt.setp(lines) 查看 lines 可以设置的属性,各属性的含义可参考 matplotlib 的文档。

In [11]:

  1. plt.setp(lines)
  1. agg_filter: unknown
  2. alpha: float (0.0 transparent through 1.0 opaque)
  3. animated: [True | False]
  4. antialiased or aa: [True | False]
  5. axes: an :class:`~matplotlib.axes.Axes` instance
  6. clip_box: a :class:`matplotlib.transforms.Bbox` instance
  7. clip_on: [True | False]
  8. clip_path: [ (:class:`~matplotlib.path.Path`, :class:`~matplotlib.transforms.Transform`) | :class:`~matplotlib.patches.Patch` | None ]
  9. color or c: any matplotlib color
  10. contains: a callable function
  11. dash_capstyle: ['butt' | 'round' | 'projecting']
  12. dash_joinstyle: ['miter' | 'round' | 'bevel']
  13. dashes: sequence of on/off ink in points
  14. drawstyle: ['default' | 'steps' | 'steps-pre' | 'steps-mid' | 'steps-post']
  15. figure: a :class:`matplotlib.figure.Figure` instance
  16. fillstyle: ['full' | 'left' | 'right' | 'bottom' | 'top' | 'none']
  17. gid: an id string
  18. label: string or anything printable with '%s' conversion.
  19. linestyle or ls: [``'-'`` | ``'--'`` | ``'-.'`` | ``':'`` | ``'None'`` | ``' '`` | ``''``]
  20. linewidth or lw: float value in points
  21. lod: [True | False]
  22. marker: :mod:`A valid marker style <matplotlib.markers>`
  23. markeredgecolor or mec: any matplotlib color
  24. markeredgewidth or mew: float value in points
  25. markerfacecolor or mfc: any matplotlib color
  26. markerfacecoloralt or mfcalt: any matplotlib color
  27. markersize or ms: float
  28. markevery: [None | int | length-2 tuple of int | slice | list/array of int | float | length-2 tuple of float]
  29. path_effects: unknown
  30. picker: float distance in points or callable pick function ``fn(artist, event)``
  31. pickradius: float distance in points
  32. rasterized: [True | False | None]
  33. sketch_params: unknown
  34. snap: unknown
  35. solid_capstyle: ['butt' | 'round' | 'projecting']
  36. solid_joinstyle: ['miter' | 'round' | 'bevel']
  37. transform: a :class:`matplotlib.transforms.Transform` instance
  38. url: a url string
  39. visible: [True | False]
  40. xdata: 1D array
  41. ydata: 1D array
  42. zorder: any number

子图¶

figure() 函数会产生一个指定编号为 num 的图:

  1. plt.figure(num)

这里,figure(1) 其实是可以省略的,因为默认情况下 plt 会自动产生一幅图像。

使用 subplot 可以在一副图中生成多个子图,其参数为:

  1. plt.subplot(numrows, numcols, fignum)

numrows * numcols < 10 时,中间的逗号可以省略,因此 plt.subplot(211) 就相当于 plt.subplot(2,1,1)

In [12]:

  1. def f(t):
  2. return np.exp(-t) * np.cos(2*np.pi*t)
  3.  
  4. t1 = np.arange(0.0, 5.0, 0.1)
  5. t2 = np.arange(0.0, 5.0, 0.02)
  6.  
  7. plt.figure(1)
  8. plt.subplot(211)
  9. plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')
  10.  
  11. plt.subplot(212)
  12. plt.plot(t2, np.cos(2*np.pi*t2), 'r--')
  13. plt.show()

06.01 Pyplot 教程 - 图9

图形上加上文字¶

plt.hist() 可以用来画直方图。

In [13]:

  1. mu, sigma = 100, 15
  2. x = mu + sigma * np.random.randn(10000)
  3.  
  4. # the histogram of the data
  5. n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)
  6.  
  7.  
  8. plt.xlabel('Smarts')
  9. plt.ylabel('Probability')
  10. plt.title('Histogram of IQ')
  11. plt.text(60, .025, r'$\mu=100,\ \sigma=15$')
  12. plt.axis([40, 160, 0, 0.03])
  13. plt.grid(True)
  14. plt.show()

06.01 Pyplot 教程 - 图10

对于这幅图形,我们使用 xlabelylabeltitletext 方法设置了文字,其中:

  • xlabel :x 轴标注

  • ylabel :y 轴标注

  • title :图形标题

  • text :在指定位置放入文字

输入特殊符号支持使用 Tex 语法,用 $<some Tex code>$ 隔开。

除了使用 text 在指定位置标上文字之外,还可以使用 annotate 函数进行注释,annotate 主要有两个参数:

  • xy :注释位置
  • xytext :注释文字位置

In [14]:

  1. ax = plt.subplot(111)
  2.  
  3. t = np.arange(0.0, 5.0, 0.01)
  4. s = np.cos(2*np.pi*t)
  5. line, = plt.plot(t, s, lw=2)
  6.  
  7. plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5),
  8. arrowprops=dict(facecolor='black', shrink=0.05),
  9. )
  10.  
  11. plt.ylim(-2,2)
  12. plt.show()

06.01 Pyplot 教程 - 图11

原文: https://nbviewer.jupyter.org/github/lijin-THU/notes-python/blob/master/06-matplotlib/06.01-pyplot-tutorial.ipynb