Python Matplotlib动画帧重叠 [英] Python Matplotlib animation frames are overlapping
问题描述
我正在研究我的轨道程序,目前我仅以-1023的向下(-y)速度对月球进行动画处理。动画有效,但是下一帧出现时每一帧都停留在图形上:
I am working on my orbit program, and I have currently only animated the moon with a downward (-y) velocity of -1023. The animation works, but each frame stays on the figure when the next one comes on:
这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
import math
import matplotlib.animation as animation
er = 6378100*10#m #earth radius
mr = 1737400*10#m #moon radius
em = 5.97219*10**24#kg #earth mass
mm = 7.34767309*10**22#kg #moon mass
d = 384400000#m #distance earth-moon
G = 6.67384*10**(-11) #gravity constant
mv = -1023#m/s #Moon velocity
nts = 10000 #no. time steps
def circle(r, h, k, a):
x = r*math.cos(a)+h
y = r*math.sin(a)+k
plt.scatter(x,y)
def simData():
tmax = 10000*nts
ts = 10000
x = 0.0
t = 0.0
while t < tmax:
n = 0
for i in range(120):
circle(mr, d, mv*t, n)
n = n + math.pi/60
t = t + ts
yield x, t
def simPoints(simData):
x, t = simData[0], simData[1]
time_text.set_text(time_template%(t))
line.set_data(t, x)
return line, time_text
fig = plt.figure()
ax = plt.axes(xlim=(-430000000, 430000000), ylim=(-430000000, 430000000))
line, = ax.plot([], [], 'bo', ms=10)
time_template = 'Time = %.1f s' # prints running simulation time
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
ani = animation.FuncAnimation(fig, simPoints, simData, blit=False,\
interval=10, repeat=True)
plt.show()
推荐答案
答案很简单: matplotlib
动画不会擦除帧之间的图像。关键是您自己必须更改屏幕上对象的属性。现在,当在圆
中执行 plt.scatter
时,您可以绘制带有一些新对象的新图像。
The answer is simple: matplotlib
animation does not wipe the image between frames. The point is that you yourself have to change the properties of the objects on the screen. Now you instead plot a new image with some new objects when you do the plt.scatter
in circle
.
我在您的代码中更改了几行以避免添加新对象,请参阅标有 ####
的注释行。现在,它应该会变得更加敏捷。 (即使月亮逃脱了地球的引力场。可惜。)
I changed a few lines in your code to avoid adding new objects, see the comment lines marked with ####
. Now it should be a bit snappier. (Even though the Moon is escaping Earth's gravitational field. Pity.)
import numpy as np
import matplotlib.pyplot as plt
import math
import matplotlib.animation as animation
er = 6378100*10#m #earth radius
mr = 1737400*10#m #moon radius
em = 5.97219*10**24#kg #earth mass
mm = 7.34767309*10**22#kg #moon mass
d = 384400000#m #distance earth-moon
G = 6.67384*10**(-11) #gravity constant
mv = -1023#m/s #Moon velocity
nts = 10000 #no. time steps
def circle(r, h, k, a):
x = r*math.cos(a)+h
y = r*math.sin(a)+k
#### CHANGED
moony.center = x,y
def simData():
tmax = 10000*nts
ts = 10000
x = 0.0
t = 0.0
while t < tmax:
n = 0
for i in range(120):
circle(mr, d, mv*t, n)
n = n + math.pi/60
t = t + ts
yield x, t
def simPoints(simData):
x, t = simData[0], simData[1]
time_text.set_text(time_template%(t))
line.set_data(t, x)
return line, time_text
fig = plt.figure()
ax = plt.axes(xlim=(-430000000, 430000000), ylim=(-430000000, 430000000))
#### CHANGED: a grey circle of moony dimensions to be moved around
moony = plt.Circle((0,0), mr, facecolor=(.8,.8,.8))
ax.add_artist(moony)
time_template = 'Time = %.1f s' # prints running simulation time
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)
ani = animation.FuncAnimation(fig, simPoints, simData, blit=False,\
interval=10, repeat=True)
plt.show()
当然,您可能想要创建一个圆圈也可以说明地球。如果只想绘制两个对象,则文件中不需要任何 plt.plot
命令。
Of course, you'll probably want to create a circle to illustrate Earth, as well. You do not need to have any plt.plot
commands in the file if you just want to plot two objects.
这篇关于Python Matplotlib动画帧重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!