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() fig.suptitle("BubbleSort") ax = plt.axes(xlim=(0, 30), ylim=(0, 100)) X = np.arange(30) Y = np.random.rand(30) * 100 print Y fun = plt.bar(X, Y, width = 0.2, facecolor = '#9999ff', edgecolor = 'white');
line, = ax.plot([], [], lw=2)
# initialization function: plot the background of each frame definit(): fun = plt.bar(X, Y, width=0.2, facecolor='#9999ff', edgecolor='white'); return fun
# animation function. This is called sequentially # note: i is framenumber defupdate(i): for y in range(30 - i - 1): if Y[y] < Y[y + 1]: Y[y], Y[y + 1] = Y[y + 1],Y[y] plt.clf() fig.suptitle("BubbleSort_FAST") fun = plt.bar(X, Y, width=0.2, facecolor='#9999ff', edgecolor='white'); return fun
# call the animator. blit=True means only re-draw the parts that have changed. anim = animation.FuncAnimation(fig, update, init_func=init, frames=30, interval=20, blit=False)
plt.show()
可以创建多个子图像,多个(坐标轴, 标题, 标签, 刻度, 刻度label)
可以加上legend(图例)
连点
1 2 3 4 5
from matplotlib import pyplot as plt x = [1,2,3,1] y = [1,3,0,1] plt.plot(x,y) plt.grid(True, color = 'r')