Issue
I've already tried to decrease the interval
argument which, by the way, only plotted to values greater than 1. When I tried interval=0.05
, for example, the pycharm generated an empty plot. I also wanted to know how to save these animations, probably, in gif format. The code currently looks like this:
fig= plt.figure()
plt.xlabel("x/M")
plt.ylabel("y/M")
circle = Circle((0, 0), 3 * math.sqrt(3), color='dimgrey')
plt.gca().add_patch(circle)
plt.gca().set_aspect('equal')
ax = plt.axes()
ax.set_facecolor("black")
plt.axis([-1 / u1 - 5, 1 / u1 + 5, -1 / u1 - 5, 1 / u1 + 5])
graph, = plt.plot([],[],'o', color="gold", markersize=2)
def animate(i):
graph.set_data(x[:i+1],y[:i+1])
return graph
ani = FuncAnimation(fig, animate, repeat=False, interval=1)
ani.save('orbita.gif', writer='imagemagick', fps=30)
Solution
The interval
is for the speed on screen when generating the animation. It is measured in milliseconds, and should be an integer. Setting interval=1
is already faster than most computers can show the frames.
To change the speed of the .gif
, you need the fps
parameter of ´ani.save´, for example anim.save('test.gif', writer='imagemagick', fps=60)
would be 60 frames per second, so 60 images in 1 second. (100 fps is the maximum that the .gif
format allows, but most applications can't show images that fast, nor do most screens update that fast. Therefore, 30 fps is usually a good choice.)
If your data is very long, you might want to use less frames, and add more points in one animate()
step. Here is an example that adds 20 points at each step.
graph, = plt.plot([],[],'o', color="gold", markersize=2)
def animate(i):
graph.set_data(x[:i*20],y[:i*20])
return graph, # the comma is needed here
ani = FuncAnimation(fig, animate, repeat=False, interval=1, frames=len(x)//20)
ani.save('orbita.gif', writer='imagemagick', fps=30)
Also make sure that your x
and y
arrays correctly contain the points of the curve you want to show. And that these points lie within the coordinates set by plt.axis([-1 / u1 - 5, 1 / u1 + 5, -1 / u1 - 5, 1 / u1 + 5])
. As a check, you might want to draw just the curve, without animation.
Answered By - JohanC
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.