Matplotlib 简明教程
Matplotlib - PyLab module
PyLab 是面向 Matplotlib 面向对象绘图库的过程化界面。Matplotlib 是整个包;matplotlib.pyplot 是 Matplotlib 中的一个模块;PyLab 与 Matplotlib 一起安装的一个模块。
PyLab is a procedural interface to the Matplotlib object-oriented plotting library. Matplotlib is the whole package; matplotlib.pyplot is a module in Matplotlib; and PyLab is a module that gets installed alongside Matplotlib.
PyLab 是一个方便的模块,它以单个名称空间大量导入 matplotlib.pyplot(用于绘图)和 NumPy(用于数学和处理数组)。尽管许多示例使用 PyLab,但不再建议使用。
PyLab is a convenience module that bulk imports matplotlib.pyplot (for plotting) and NumPy (for Mathematics and working with arrays) in a single name space. Although many examples use PyLab, it is no longer recommended.
Basic Plotting
可以使用 plot 命令对曲线进行绘制。它采用一对等长数组(或序列)-
Plotting curves is done with the plot command. It takes a pair of same-length arrays (or sequences) −
from numpy import *
from pylab import *
x = linspace(-3, 3, 30)
y = x**2
plot(x, y)
show()
上述代码行生成以下输出 -
The above line of code generates the following output −

要绘制符号而非线条,请提供一个附加的字符串参数。
To plot symbols rather than lines, provide an additional string argument.
symbols |
- , –, -., , . , , , o , ^ , v , < , > , s , + , x , D , d , 1 , 2 , 3 , 4 , h , H , p , |
, _ |
现在,考虑执行以下代码 -
Now, consider executing the following code −
from pylab import *
x = linspace(-3, 3, 30)
y = x**2
plot(x, y, 'r.')
show()
它以红色圆点形式显示图,如下所示-
It plots the red dots as shown below −

可以叠加绘图。只需使用多个绘图命令。使用 clf() 清除绘图。
Plots can be overlaid. Just use the multiple plot commands. Use clf() to clear the plot.
from pylab import *
plot(x, sin(x))
plot(x, cos(x), 'r-')
plot(x, -sin(x), 'g--')
show()
上述代码行生成以下输出 -
The above line of code generates the following output −
