#!/usr/bin/env python # coding: utf-8 # # Pyplot 教程 # ## Matplotlib 简介 # **`matplotlib`** 是一个 **`Python`** 的 `2D` 图形包。 # # 在线文档:http://matplotlib.org ,提供了 [Examples](http://matplotlib.org/examples/index.html), [FAQ](http://matplotlib.org/faq/index.html), [API](http://matplotlib.org/contents.html), [Gallery](http://matplotlib.org/gallery.html),其中 [Gallery](http://matplotlib.org/gallery.html) 是很有用的一个部分,因为它提供了各种画图方式的可视化,方便用户根据需求进行选择。 # ## 使用 Pyplot # 导入相关的包: # In[1]: import numpy as np 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]: get_ipython().run_line_magic('matplotlib', 'inline') # ## plt.plot() 函数 # ### 例子 # `plt.plot()` 函数可以用来绘图: # In[3]: plt.plot([1,2,3,4]) plt.ylabel('some numbers') plt.show() # ### 基本用法 # `plot` 函数基本的用法有以下四种: # # 默认参数 # - `plt.plot(x,y)` # # 指定参数 # - `plt.plot(x,y, format_str)` # # 默认参数,`x` 为 `0~N-1` # - `plt.plot(y)` # # 指定参数,`x` 为 `0~N-1` # - `plt.plot(y, format_str)` # # 因此,在上面的例子中,我们没有给定 `x` 的值,所以其默认值为 `[0,1,2,3]`。 # # 传入 `x` 和 `y`: # In[4]: plt.plot([1,2,3,4], [1,4,9,16]) # ### 字符参数 # 和 **`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]: plt.plot([1,2,3,4], [1,4,9,16], 'ro') plt.show() # 可以看出,有两个点在图像的边缘,因此,我们需要改变轴的显示范围。 # ### 显示范围 # 与 **`MATLAB`** 类似,这里可以使用 `axis` 函数指定坐标轴显示的范围: # # plt.axis([xmin, xmax, ymin, ymax]) # In[6]: plt.plot([1,2,3,4], [1,4,9,16], 'ro') # 指定 x 轴显示区域为 0-6,y 轴为 0-20 plt.axis([0,6,0,20]) plt.show() # ### 传入 `Numpy` 数组 # 之前我们传给 `plot` 的参数都是列表,事实上,向 `plot` 中传入 `numpy` 数组是更常用的做法。事实上,如果传入的是列表,`matplotlib` 会在内部将它转化成数组再进行处理: # In[7]: import numpy as np import matplotlib.pyplot as plt # evenly sampled time at 200ms intervals t = np.arange(0., 5., 0.2) # red dashes, blue squares and green triangles plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^') plt.show() # ### 传入多组数据 # 事实上,在上面的例子中,我们不仅仅向 `plot` 函数传入了数组,还传入了多组 `(x,y,format_str)` 参数,它们在同一张图上显示。 # # 这意味着我们不需要使用多个 `plot` 函数来画多组数组,只需要可以将这些组合放到一个 `plot` 函数中去即可。 # ### 线条属性 # 之前提到,我们可以用字符串来控制线条的属性,事实上还可以通过关键词来改变线条的性质,例如 `linwidth` 可以改变线条的宽度,`color` 可以改变线条的颜色: # In[8]: x = np.linspace(-np.pi,np.pi) y = np.sin(x) plt.plot(x, y, linewidth=2.0, color='r') plt.show() # ### 使用 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]: # 加逗号 line 中得到的是 line2D 对象,不加逗号得到的是只有一个 line2D 对象的列表 line, = plt.plot(x, y, 'r-') # 将抗锯齿关闭 line.set_antialiased(False) plt.show() # ### plt.setp() 修改线条性质 # 更方便的做法是使用 `plt` 的 `setp` 函数: # In[10]: lines = plt.plot(x, y) # 使用键值对 plt.setp(lines, color='r', linewidth=2.0) # 或者使用 MATLAB 风格的字符串对 plt.setp(lines, 'color', 'r', 'linewidth', 2.0) plt.show() # 可以设置的属性有很多,可以使用 `plt.setp(lines)` 查看 `lines` 可以设置的属性,各属性的含义可参考 `matplotlib` 的文档。 # In[11]: plt.setp(lines) # ## 子图 # `figure()` 函数会产生一个指定编号为 `num` 的图: # # plt.figure(num) # # 这里,`figure(1)` 其实是可以省略的,因为默认情况下 `plt` 会自动产生一幅图像。 # # 使用 `subplot` 可以在一副图中生成多个子图,其参数为: # # plt.subplot(numrows, numcols, fignum) # # 当 `numrows * numcols < 10` 时,中间的逗号可以省略,因此 `plt.subplot(211)` 就相当于 `plt.subplot(2,1,1)`。 # In[12]: def f(t): return np.exp(-t) * np.cos(2*np.pi*t) t1 = np.arange(0.0, 5.0, 0.1) t2 = np.arange(0.0, 5.0, 0.02) plt.figure(1) plt.subplot(211) plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k') plt.subplot(212) plt.plot(t2, np.cos(2*np.pi*t2), 'r--') plt.show() # ## 图形上加上文字 # `plt.hist()` 可以用来画直方图。 # In[13]: mu, sigma = 100, 15 x = mu + sigma * np.random.randn(10000) # the histogram of the data n, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75) plt.xlabel('Smarts') plt.ylabel('Probability') plt.title('Histogram of IQ') plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show() # 对于这幅图形,我们使用 `xlabel` ,`ylabel`,`title`,`text` 方法设置了文字,其中: # # - `xlabel` :x 轴标注 # # - `ylabel` :y 轴标注 # # - `title` :图形标题 # # - `text` :在指定位置放入文字 # # 输入特殊符号支持使用 `Tex` 语法,用 `$$` 隔开。 # # 除了使用 `text` 在指定位置标上文字之外,还可以使用 `annotate` 函数进行注释,`annotate` 主要有两个参数: # # - `xy` :注释位置 # - `xytext` :注释文字位置 # In[14]: ax = plt.subplot(111) t = np.arange(0.0, 5.0, 0.01) s = np.cos(2*np.pi*t) line, = plt.plot(t, s, lw=2) plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2,2) plt.show()