1

I am trying to follow the basic animation tutorial located here and adapting it to display an already computed dataset instead of evaluating a function every frame, but am getting stuck. My dataset involves XY coordinates over time, contained in the lists satxpos and satypos I am trying to create an animation such that it traces a line starting at the beginning of the dataset through the end, displaying say 1 new point every 0.1 seconds. Any help with where I'm going wrong?

from matplotlib import pyplot as plt
from matplotlib import animation
import numpy as np

Code here creates satxpos and satypos as lists

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

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

def animate(i):
    line.set_data(satxpos[i], satypos[i])
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames = len(satxpos), interval = 1, blit=True)

Edit: The code runs without errors, but generates a blank plot window with no points/lines displayed and nothing animates. The dataset is generated correctly and views fine in a static plot.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
c.wen
  • 35
  • 6
  • Is this all of your code? Are you getting any error messages when you run it? – cosinepenguin Jul 17 '17 at 23:16
  • It is not all the code, the parts that generate satxpos and satypos do create valid datasets. I can view those as a static plot just fine. Code runs without errors, but the plot thats generated is just a blank window, no animation or points/lines are displayed – c.wen Jul 17 '17 at 23:21
  • Do you use `anim.save()` and `plt.show()` at some point in the code you didn't share? – cosinepenguin Jul 17 '17 at 23:23
  • I actually didn't have `plt.show()`, however the plot window came up anyway. I tried adding it, and no difference.running the example code exhibits the same behavior, but the example code animates. I did not use `anim.save` as I am not interested in writing the animation to a file for now, just viewing it. – c.wen Jul 17 '17 at 23:29
  • And the elements in `satxpos` and `satypos` are `np` `numpy` objects? – cosinepenguin Jul 17 '17 at 23:37
  • Yes satxpos and satypos are arrays, as that is what np.linspace creates – c.wen Jul 17 '17 at 23:48
  • 1
    `satxpos` and `satxpos` cannot be just `np.linspace`. It won't work. It has to be a 2-D array where each row is one `np.linspace`. I provided a valid example of `satxpos` and `satxpos` and some trouble shooting ideas in my answer. – Y. Luo Jul 17 '17 at 23:50
  • 2
    I bet if you use `line, =ax.plot([], [], 'o')` you will see a single point moving around – MaxNoe Jul 17 '17 at 23:52

3 Answers3

5

In order to "trace a line starting at the beginning of the dataset through the end" you would index your arrays to contain one more element per timestep:

line.set_data(satxpos[:i], satypos[:i])

(Note the :!)

Everything else in the code looks fine, such that with the above manipulation you should get and extending line plot. You might then want to set interval to something greater than 1, as that would mean 1 millesecond timesteps (which might be a bit too fast). I guess using interval = 40 might be a good start.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
1

Your code looks correct! So long as satxpos and satypos are both configured and initialized properly, I believe everything else is valid!

One part of the code you do not show in your question is the invocation of the anim.save() and plt.show() functions, which are both necessary for your code to work (as per the tutorial you shared!)

You would therefore need to add something like:

anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264'])

plt.show()

to the end of your code to create the animation (and show it, I presume)!

Hope it helps!

Source - Matplotlib Animation Tutorial

cosinepenguin
  • 1,545
  • 1
  • 12
  • 21
1

I saw you mentioned "the parts that generate satxpos and satypos do create valid datasets. I can view those as a static plot just fine". But my guess is still the problem originated from your satxpos and satypos.

One way you can trouble shoot is to replace your two functions and animation code with line.set_data(satxpos[i], satypos[i]). Set i as 0, 1, ... and see if you can see the plot. If not, your satxpos and satypos are not as valid as you claimed.

As an example, a valid satxpos and satypos can be like this:

x = np.array([np.linspace(-1e7, 1e7, 1000)])
i = 200
satxpos = x.repeat(i, axis=0)
satypos = np.sin(2 * np.pi * (satxpos - 0.01 * np.arange(i).reshape(-1, 1).repeat(satxpos.shape[1], axis=1)))
satypos *= 1e7 / 2

This works with the code you provided thus indicating the code you've shown us is fine.

Edit in response to comments:

If your satxpos and satypos are just np.linespace, the animation loop will get just one point with (satxpos[i], satypos[i]) and you won't see the point on the plot without a setting like marker='o'. Therefore, you see nothing in your animation.

Community
  • 1
  • 1
Y. Luo
  • 5,622
  • 1
  • 18
  • 25