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

标签 python animation matplotlib scipy

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

您之前的尝试是设置 xy 值,然后用新的 xy,然后再做一次。

关于python - Matplotlib error Line2d object s not iterable error in tkinter callback nothing shows up,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31642150/

相关文章:

python - 在 Python 中缩放和显示图像的最快方法是什么?

javascript - Odoo:odoo TreeView 标题中的自定义按钮不会触发 python 函数

python - 如何确定一列是否包含 Pandas 中的某些元素

python - while 在 for 循环中循环获取列表列表

javascript - javascript 中的 ON/OFF css 类 - 用于动画处理

jquery.animate(),奇怪的行为

javascript - 使用与 div 相关的 scrollmagic 固定元素

python - 使用循环从列表创建子图。使数据绘制到两个子图上

python - Matplotlib 查看后自动关闭绘图/图形

python - 将 Python 列表转换为数据框