【问题标题】:matplotlib animation with scatter带有散点图的 matplotlib 动画
【发布时间】:2020-09-21 04:21:24
【问题描述】:

我有以下代码:

sim = simulator()
fig, ax = plt.subplots()
scat = ax.scatter(sim.XY[:,0],sim.XY[:,1])

def animate(i):
    sim.run()
    scat.set_offsets(sim.XY)  # update the data.  
    return scat

ani = FuncAnimation(fig, animate, interval=10, blit=True, save_count=50)

“sim”对象方法“sim.run()”更新 sim.XY,即 (100,2) 数组。

在我的第三行之后,我得到了预期的散点图。

但是当我尝试制作动画时(从第 5 行向下),我收​​到一条错误消息。有人可以帮忙吗:

Traceback (most recent call last):
  File "C:\Users\anaconda3\lib\site-packages\matplotlib\cbook\__init__.py", line 216, in process
    func(*args, **kwargs)
  File "C:\Users\anaconda3\lib\site-packages\matplotlib\animation.py", line 953, in _start
    self._init_draw()
  File "C:\Users\\anaconda3\lib\site-packages\matplotlib\animation.py", line 1732, in _init_draw
    self._draw_frame(next(self.new_frame_seq()))
  File "C:\Users\anaconda3\lib\site-packages\matplotlib\animation.py", line 1761, in _draw_frame
    key=lambda x: x.get_zorder())

TypeError: 'PathCollection' object is not iterable

【问题讨论】:

标签: python matplotlib animation scatter-plot


【解决方案1】:

我观察到的是,如果我设置 blit=True,动画将不起作用并且我收到错误消息。没有参数,动画就可以工作。有人可以解释一下该参数在做什么

【讨论】:

  • blit=true 仅重绘已更改的绘图元素。
【解决方案2】:

如果你在 scat 后面加一个逗号,我相信它应该可以工作:

def animate(i):
    sim.run()
    scat.set_offsets(sim.XY)  # update the data.  
    return scat,

当 blit=True 时,输出预期为可迭代:

请看这里:https://matplotlib.org/stable/api/_as_gen/matplotlib.animation.FuncAnimation.html

如果 blit == True,func 必须返回所有艺术家的迭代 修改或创建。此信息由 blitting 使用 算法来确定图形的哪些部分必须更新。

在 scat 后加一个逗号,使其成为可迭代对象。

【讨论】:

    猜你喜欢
    • 2021-11-05
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 2021-10-21
    • 2013-01-22
    相关资源
    最近更新 更多