【发布时间】:2014-03-23 04:14:45
【问题描述】:
Thanks to Jake Vanderplas,我知道如何使用matplotlib 开始编写动画情节。这是一个示例代码:
from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(0, 100))
line, = plt.plot([], [])
def init():
line.set_data([], [])
return line,
def animate(i):
line.set_data([0, 2], [0,i])
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=100, interval=20, blit=True)
plt.show()
假设现在我想绘制大量函数(这里说四个),在循环的帮助下定义。我做了一些伏都教编程,试图了解如何模仿逗号后面的行,这就是我得到的(不用说它不起作用:AttributeError: 'tuple' object has no attribute 'axes')。
from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(0, 100))
line = []
N = 4
for j in range(N):
temp, = plt.plot([], [])
line.append(temp)
line = tuple(line)
def init():
for j in range(N):
line[j].set_data([], [])
return line,
def animate(i):
for j in range(N):
line[j].set_data([0, 2], [10 * j,i])
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=100, interval=20, blit=True)
plt.show()
我的一些问题是:我怎样才能让它发挥作用?奖励(可能链接):line, = plt.plot([], []) 和 line = plt.plot([], []) 有什么区别?
谢谢
【问题讨论】:
标签: python animation matplotlib