簡體   English   中英

matplotlib animation 帶散點

[英]matplotlib animation with scatter

我有以下代碼:

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" object 方法 "sim.run()" 更新 sim.XY 是 (100,2) 數組。

在我的第三行之后,我得到了一個分散的 plot 正如預期的那樣。

但是當我嘗試制作動畫時(從第 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

我觀察到的是,如果我設置 blit=True,animation 不起作用,我得到錯誤。 沒有參數 animation 可以工作。 有人可以解釋一下那個參數在做什么嗎

如果你在 scat 之后加一個逗號,我相信它應該可以工作:

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

當 blit=True 時,output 預計是可迭代的:

見這里: https://matplotlib.org/stable/api/_as_gen/matplotlib.animation.FuncAnimation.html

如果 blit == True,func 必須返回所有被修改或創建的藝術家的迭代。 位圖算法使用此信息來確定圖形的哪些部分必須更新。

在 scat 后加逗號,使其成為可迭代的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM