0

I have the following code:

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)

The "sim" object method "sim.run()" updates sim.XY which is (100,2) array.

After my 3rd line I get a scatter plot as expected.

But when I try to animate (from line 5 downwards) I get a error message saying. Can someone help please:

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
Anwarvic
  • 12,156
  • 4
  • 49
  • 69
user1612986
  • 1,373
  • 3
  • 22
  • 38
  • Check this out: https://stackoverflow.com/questions/39984509/typeerror-pathcollection-object-is-not-iterable-when-adding-second-legend-to – Anwarvic Jun 02 '20 at 16:38
  • @Anwarvic that post may not be relevant. I am getting an error when i try to animate. I took care of the multiple return issue issue that post cites. – user1612986 Jun 02 '20 at 16:42
  • Does this answer your question? [How to animate a scatter plot?](https://stackoverflow.com/questions/9401658/how-to-animate-a-scatter-plot) – Red Jun 02 '20 at 18:59
  • @AnnZen Not directly. I have found the issue. If I set blit=True the animation does not work and I get the error. Without the parameter the animation works. Can someone explain please what that parameter is doing. . – user1612986 Jun 02 '20 at 20:13
  • I think this is the answer to your question: https://stackoverflow.com/questions/35068396/matplotlib-funcanimation-error-when-blit-true – panadestein Jun 02 '20 at 20:29

2 Answers2

0

What I observed is If I set blit=True the animation does not work and I get the error. Without the parameter the animation works. Can someone explain please what that parameter is doing

user1612986
  • 1,373
  • 3
  • 22
  • 38
0

If you put a comma after scat, I believe it should work:

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

When blit=True, the output is expected to be an iterable:

See here: https://matplotlib.org/stable/api/_as_gen/matplotlib.animation.FuncAnimation.html

If blit == True, func must return an iterable of all artists that were modified or created. This information is used by the blitting algorithm to determine which parts of the figure have to be updated.

Putting a comma after scat, makes it an iterable.

Chachni
  • 427
  • 4
  • 11