2

我正在研究我的轨道计划,我目前只以 -1023 的向下 (-y) 速度为月球设置动画。动画有效,但是当下一帧出现时,每一帧都停留在图形上: 在此处输入图像描述

这是我的代码:

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

1 回答 1

3

答案很简单:matplotlib动画不会在帧之间擦除图像。关键是您自己必须更改屏幕上对象的属性。现在,当您执行plt.scatterin时,您改为使用一些新对象绘制一个新图像circle

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

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如果您只想绘制两个对象,则不需要在文件中包含任何命令。

于 2014-07-09T19:42:59.630 回答