matplotlib.pyplot是一些命令行风格函数的集合,使matplotlib以类似于MATLAB的方式工作。每个pyplot函数对一幅图片(figure)做一些改动:比如创建新图片,在图片创建一个新的作图区域(plotting area),在一个作图区域内画直线,给图添加标签(label)等。matplotlib.pyplot是有状态的,亦即它会保存当前图片和作图区域的状态,新的作图函数会作用在当前图片的状态基础之上。
(1)使用plot()函数画图
plot()为画线函数,下面的小例子给ploy()一个列表数据[1,2,3,4],matplotlib假设它是y轴的数值序列,然后会自动产生x轴的值,因为python是从0作为起始的,所以这里x轴的序列对应为[0,1,2,3]。
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('numbers') #给y轴加注释
plt.show()
plot()还可以接受x,y成对的参数,还有一个可选的参数是表示线的标记和颜色,plot函数默认画线是蓝色实线,即字符串'b-',你可以选择自己喜欢的标记和颜色。
import matplotlib.pyplot as plt
plt.plot([1,2,3,4], [1,8,27,64], 'ro')
plt.axis([0, 6, 0, 80])
plt.show()
axis()函数给出了形如[xmin,xmax,ymin,ymax]的列表,指定了坐标轴的范围。给出一个numpy数组(arrays),给出不同的线。
import numpy as np
import matplotlib.pyplot as plt
f= np.arange(0, 5, 0.1)
# red dashes, blue squares and green triangles
plt.plot(f, f, 'r--', f, f**2, 'bs', f, f**3, 'g^')
plt.show()
(2)线的属性
可以用不同方式对线的属性进行设置:
用参数的关键字:plt.plot(x, y, linewidth=2.0)通过这种方式修改线宽;
使用Line2D实例的set方法:
plot函数返回一个线的列表,比如line1,line2 = plot(x1,y1,x2,y2)。由于我们只有一条直线,对于长度为1的列表(list),我们可以用逗号,来得到列表第一个元素
使用pyplot的setp()命令
还可以用setp()命令来进行设置,该命令可以对一个列表或者单个对象进行设置,并且提供了matlab式的使用方法
(3)多个图像
pyplot和MATLAB一样,都有当前图像和当前坐标的概念,所有命令都是对当前的坐标进行设置。
import numpy as np
import matplotlib.pyplot as plt
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()
subplot()命令会指定一个坐标系,默认是subplot(111),111参数分别说明行的数目numrows,列的数目numcols,第几个图像fignum(fignum的范围从1到numrows*numcols)。
subplot(211)指定显示两行,每行一图,接下来为第一幅图像。
(4)为图像做文本说明
text()命令可以用于在任意位置添加文本,而xlabel(),ylabel(),title()用来在指定位置添加文本。所有的text()命令返回一个matplotlib.text.Text实例,也可以通过关键字或者setp()函数对文本的属性进行设置。
import numpy as np
import pylab as pl
data = np.random.normal(0,2,100)
# make a histogram of the data array
pl.hist(data)
# make plot labels
pl.xlabel('distribute')
pl.ylabel('Probability')
pl.title('Histogram')
pl.text(0, 40, r'$\mu=0,\ \sigma=2$')
pl.axis([-10, 10, 0, 50])
#pl.grid(True)
pl.show()
matplotlib从1.1.0版本以后就开始支持绘制动画,网取的示例:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line, = ax.plot([], [], lw=2)
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
return line,
# animation function. This is called sequentially
# note: i is framenumber
def animate(i):
x = np.linspace(0, 2, 1000)
y = np.sin(2 * np.pi * (x - 0.01 * i))
line.set_data(x, y)
return line,
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True)
#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
plt.show()
关于导出GIF动态图,参见:http://www.hankcs.com/ml/using-matplotlib-and-imagemagick-to-realize-the-algorithm-visualization-and-gif-export.html,
还没试过。
参考:
http://www.tuicool.com/articles/7zYNZfI