python做flash帧动画_Python Matplotlib动画帧重叠

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:

Here is my code:

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()

解决方案

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()

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值