0

I currently have an array G = [x,y,t] where each spatial point (G[0][i], G[1][i]) has a time component t = G[2][i]. The array is sorted by time. I am trying to animate the scatter plot so points show up in chronological order and do not disappear. Here is my current code:

from matplotlib.animation import FuncAnimation
import matplotlib.animation as animation

fig = plt.figure(figsize=(10,7))
ax = plt.subplot(111, xlim=(0,1), ylim=(0,1))

def animationUpdate(k):
    x = G[0][:k]
    y = G[1][:k]
    scat.set_offsets(np.c_[x,y])
    return scat

anim = FuncAnimation(fig, animationUpdate, frames=10, interval=100, blit=True)

I get the error "'PathCollection' object is not iterable" which I am not sure how to fix. I am also unsure how to arrange it so the points show up with respect to their time component. Do I modify the frames or interval section of FuncAnimation? Thanks!

Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • For a scatter plot, FuncAnimation is often not necessary. [See an interactive plot for instance here as example (a)](https://stackoverflow.com/a/42738014/8881141). The most important part here is *drumroll*: `plt.pause(0.1)` – Mr. T Feb 11 '21 at 19:34
  • @Mr.T I tried using the code from (a) and (b). In both instances it seems to only plot a single point then not do anything. Any further help? – mathguy424 Feb 11 '21 at 20:21
  • What is your matplotlib version? What is your backend? Any specific environment you run this in that might interfere (Jupyter, Spyder, PyCharm, you name it)? – Mr. T Feb 11 '21 at 20:22
  • @Mr.T I'm currently running this in Google Collab, I can try running it in Jupyter. Not sure what version or how to check – mathguy424 Feb 11 '21 at 20:25
  • @Mr.T It does the same in Jupyter, and the version of matplotlib is 3.1.3 – mathguy424 Feb 11 '21 at 20:31
  • I don't know Jupyter but maybe this helps? https://stackoverflow.com/a/46878070/8881141 – Mr. T Feb 11 '21 at 20:34
  • @Mr.T I got it working by saving it as a file. Must be something with Jupyter or whatever because a pre-coded plot I found online wouldn't animate in the environment either. Thanks for the help. – mathguy424 Feb 11 '21 at 21:17

0 Answers0