【发布时间】:2014-01-12 16:56:14
【问题描述】:
我正在尝试为演示文稿制作动画数据。我正在尝试使用 python 的动画包来做到这一点。我正在尝试做的事情大致归结为http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/中的第一个示例
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
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)
我是 python 新手,所以我很难理解正在发生的事情。对我来说,似乎 init() 和 animate(i) 都没有修改它们返回的内容。此外,他们正在修改的对象 line(而不是 line)之前没有声明过。
无论哪种方式,我想要做的是获得数据,在这种情况下是正弦波,分段着色。蓝色介于 0 和 1 之间,红色介于 1 和 1.5 之间,蓝色又介于 1.5 和 2 之间。我尝试了很多东西,但无法让它发挥作用。我尝试让这些函数返回整个图形,而不仅仅是线,希望它们会刷新预览图形并绘制我绘制的复合线,但无济于事。
在这个框架中,我如何为由几段(不同颜色的)线组成的线制作动画?
【问题讨论】:
标签: python animation matplotlib