【问题标题】:Matplotlib animation - tuple object is not callableMatplotlib 动画 - 元组对象不可调用
【发布时间】:2014-09-09 11:09:11
【问题描述】:

matplotlib 中的 FuncAnimation 函数存在问题。代码如下:

import time
from matplotlib import pyplot as plt
from matplotlib import animation
from ppbase import *

plt.ion()

#This is just essentially a stream of data
Paramater = PbaProObject("address of object")

fig = plt.figure()
ax = plt.axes(xlim=(0,2), ylim=(-90, 90))
line, = ax.plot([], [], lw=2)

def init():
  line.set_data([], [])
  return line, 

def animate(Parameter):
  x = time.time()
  y = Parameter.ValueStr
  line.set_data(x, y)
  return line, 

anim = animation.FuncAnimation(fig, animate(Parameter), init_func=init,
                               frames=200, interval=2, blit=True)
plt.show()

错误是:

Traceback (most recent call last):
File "C:\Anaconda\lib\site-packages\matplotlib\backend_bases.py", line 1203, in _on_timer
ret = func(*args, **kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\animation.py", line 876, in _step
still_going = Animation._step(self, *args)
File "C:\Anaconda\lib\site-packages\matplotlib\animation.py", line 735, in _step self._draw_frame(framedata)
File "C:\Anaconda\lib\site-packages\matplotlib\animation.py", line 754, in _draw_next_frame self._draw_frame(framedata, self._blit)
File "C:\Anaconda\lib\site-packages\matplotlib\animation.py", line 1049, in _draw_frame self._drawn_artists = self._func(framedata, *self._args)
TypeError: 'tuple' object is not callable

整个上午都在阅读,似乎 plt.plot 通常被一个元组覆盖,所以我检查了它,但我认为我没有在任何地方做过。我也把 blit 变成了假,但这也没有帮助。我也想以交互方式更新 x 轴,我有一行: ax = plt.axes(xlim((x-10), (x+10)), ylim=(-90, 90)) 在 animate 函数中,但把它拿出来看看它是否有任何区别。

我认为问题主要源于对元组的理解不够好。另外,我对 FuncAnimation 函数的理解是,它每次更新绘图时都会调用 animate() - 因此我认为我也可以使用它来更新轴。但情况可能并非如此。

任何帮助表示赞赏。

【问题讨论】:

  • 尝试将return line,替换为return line
  • "a," 是 "(a,)" 的简写——即它是一个长度为 1 的元组。
  • 改变了,我得到了同样的错误,但最后一点是:TypeError: 'line2D' object is not callable
  • 啊!但这是另一个问题。
  • 感谢您的回复。我用谷歌搜索了这个新错误,但仍然无法找出可能是什么问题,你知道吗?

标签: python matplotlib jquery-animate tuples


【解决方案1】:

您需要传入函数 object 而不是调用函数的结果,并且可以将生成器传递给 frames(这可能仅适用于 1.4.0+)。

# turn your Parameter object into a generator
def param_gen(p):
    yield p.ValueStr

def animate(p):
    # get the data the is currently on the graph
    old_x = line.get_xdata()
    old_y = line.get_ydata()
    # add the new data to the end of the old data
    x = np.r_[old_x, time.time()]
    y = np.r_[old_y, p]
    # update the data in the line
    line.set_data(x, y)
    # return the line2D object so the blitting code knows what to redraw
    return line, 


anim = animation.FuncAnimation(fig, animate, init_func=init,
                           frames=param_gen(Parameter), interval=2, blit=True)

我还修复了您的动画功能的一个问题,您应该使用 4 个空格缩进,而不是 2 个。

【讨论】:

  • 感谢您的回复。你能解释一下你的改变是做什么的以及你为什么建议它们吗?我想完全了解原因,以便将来解决自己的问题。我的程序仍然无法运行,我得到一个 TypeError: Unhashable type: 'numpy.ndarray'。我觉得我越来越近了,所以理解这个修复可能会对我有所帮助。 np.r_ 的目的是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-29
  • 2021-09-30
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多