Skip to main content
 首页 » 编程设计

python之Matplotlib error Line2d object s not iterable error in tkinter callback nothing shows up

2025年05月04日16xxx_UU

代码如下所示。我正在尝试使用之前计算的矢量制作动画,打开图形窗口所以我知道它已经走到这一步并且矢量正在正确计算。但是 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): 

最小示例

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() 

请您参考如下方法:

这里的问题出在你的animate函数,你正在使用 set_data多次不做你认为它做的事。当它是一个集合时,您可以像追加一样使用它。参数应该是两个数组,包含该行各自的 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,然后再做一次。