代码如下所示。我试图使用先前计算的矢量动画,打开一个数字窗口,所以我知道它得到了这么远,矢量正在被正确地煅烧。但matplotlib除了图形窗口外什么也不知道为什么。请帮忙。
#finally animateing
fig = plt.figure()
ax = plt.axes(xlim = (-1000,1000) ,ylim = (-1000,1000))#limits were arbitrary
#line = ax.plot([],[])
line, = ax.plot([], [], lw=2)
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
return line,
def animate(i):
x = time_vec[i]
y = complex_vec[i]
#y1 = real_vec[i]
#y2 = modulus_vec[i]
line.set_data(x,y)
#line.set_data(x,y1)
#line.set_data(x,y2)
return line,
animation_object = animation.FuncAnimation(fig, animate, init_func= init, frames = num_files,interval = 30, blit = True)
#turnn this line on to save as mp4
#anim.save("give it a name.mp4", fps = 30, extra-args = ['vcodec', 'libx264'])
plt.show()
以下显示完整的错误消息
Traceback (most recent call last):
File "the_animation.py", line 71, in <module>
plt.show()
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 145, in show
_show(*args, **kw)
File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 117, in __call__
self.mainloop()
File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 69, in mainloop
Tk.mainloop()
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 366, in mainloop
_default_root.tk.mainloop(n)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1484, in __call__
def __call__(self, *args):
MINIMAL EXAMPLE
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
complex_vec = np.arange(5,6,.001)
real_vec = np.arange(7,8,.001)
time_vec = np.arange(0,1,.001)
num_files = np.size(time_vec)
#creating the modulus vector
modulus_vec = np.zeros(np.shape(complex_vec))
for k in range (0,complex_vec.size):
a = complex_vec[k]
b = real_vec[k]
calc_modulus = np.sqrt(a**2 + b**2)
modulus_vec[k] = calc_modulus
#finally animateing
fig = plt.figure()
ax = plt.axes(xlim = (-1000,1000) ,ylim = (-1000,1000))#limits were arbitrary
#line = ax.plot([],[])
line, = ax.plot([], [], lw=2)
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
return line,
def animate(i):
x = time_vec[i]
y = complex_vec[i]
y1 = real_vec[i]
y2 = modulus_vec[i]
line.set_data(x,y)
line.set_data(x,y1)
line.set_data(x,y2)
return line,
animation_object = animation.FuncAnimation(fig, animate, init_func= init, frames = num_files,interval = 30, blit = True)
#turnn this line on to save as mp4
#anim.save("give it a name.mp4", fps = 30, extra-args = ['vcodec', 'libx264'])
plt.show()
答案 0 :(得分:1)
此处的问题出在您的animate
函数中,您多次使用set_data
,而这与您的想法无关。当它是 set 时,你就像追加一样使用它。参数应该是两个数组,包含该行的相应x和y值。这将为您的最小示例制作动画:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
complex_vec = np.arange(5,6,.001)
real_vec = np.arange(7,8,.001)
time_vec = np.arange(0,1,.001)
num_files = np.size(time_vec)
#creating the modulus vector
modulus_vec = np.zeros(np.shape(complex_vec))
for k in range (0,complex_vec.size):
a = complex_vec[k]
b = real_vec[k]
calc_modulus = np.sqrt(a**2 + b**2)
modulus_vec[k] = calc_modulus
#finally animateing
fig = plt.figure()
ax = plt.axes(xlim = (-1,1) ,ylim = (-1,15))#limits were arbitrary
#line = ax.plot([],[])
line, = ax.plot([], [], lw=2)
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
return line,
def animate(i):
x = time_vec[i]
y = complex_vec[i]
y1 = real_vec[i]
y2 = modulus_vec[i]
# notice we are only calling set_data once, and bundling the y values into an array
line.set_data(x,np.array([y, y1, y2]))
return line,
animation_object = animation.FuncAnimation(fig,
animate,
init_func= init,
frames = num_files,
interval = 30,
blit = True)
#turnn this line on to save as mp4
#anim.save("give it a name.mp4", fps = 30, extra-args = ['vcodec', 'libx264'])
plt.show()
您之前的尝试是设置 x 和 y 值,然后使用新的 x 和 y <覆盖前一个强>,再一次这样做。