gpt4 book ai didi

Python Matplotlib 动画帧重叠

转载 作者:太空宇宙 更新时间:2023-11-04 01:16:01 25 4
gpt4 key购买 nike

我正在研究我的轨道计划,目前我只以 -1023 的向下 (-y) 速度为月球制作动画。动画有效,但下一帧出现时每一帧都停留在图形上: enter image description here

这是我的代码:

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 动画不会在帧之间删除图像。关键是您必须自己更改屏幕上对象的属性。现在,当您在 circle 中执行 plt.scatter 时,您将绘制一个带有一些新对象的新图像。

我更改了您的代码中的几行以避免添加新对象,请参阅标有#### 的注释行。现在它应该有点活泼了。 (即使月球正在逃离地球的引力场。可惜。)

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 命令。

关于Python Matplotlib 动画帧重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24659800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com