Python:我的情节没有显示任何圆圈,任何线索



绘图仅显示一个白屏,我不确定为什么。我希望行星在不同位置绕太阳绕着太阳绕,因此,我使用了Funcanimation来实现它。

from pylab import* 
from matplotlib.animation import *
fig = plt.figure()
ax = plt.axes(xlim=(-10, 10), ylim=(-10, 10), aspect=True)
x0 =0 
y0 = 0
r_s=.15
r_e = .15
r_m = .15
Sun, = plt.Circle((x0, y0), radius=r_s, ec='yellow', fc='yellow', lw=3)
Earth, = plt.Circle((2, 4), radius=r_e, ec='green', fc='green', lw=3)
Mars, = plt.Circle((1, 3), radius=r_M, ec='brown', fc='brown', lw=3)
ax.add_patch(Sun)
def init():
    ax.add_patch(Earth)
    ax.add_patch(Mars)
    return  Earth, Mars,
def animate(i):
    theta = radians(i)
    x = x0*np.cos(theta) - y0*np.sin(theta)
    y = x0*np.sin(theta) + y0*np.cos(theta)
    Earth.xy = (-x, -y)
    Earth._angle = i
    Mars.xy = (x, y)
    Mars._angle = i
    return Earth, Mars,
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=1080, 
                               interval=25, blit=True)

plt.show()

我正在玩您的代码。主要问题是(x,y)的结果始终是(0,0),因为您的值乘以sincosx0,为零。无论如何,这是对我有用的版本:

from matplotlib.animation import *
fig = plt.figure()
ax = plt.axes(xlim=(-10, 10), ylim=(-10, 10), aspect=True)
x0 =0 
y0 = 0
r_s=.15
r_e = .15
r_m = .15
d_e = 1
d_m = 2
Sun = plt.Circle((x0, y0), radius=r_s, ec='yellow', fc='yellow', lw=3)
Earth = plt.Circle((2, 4), radius=r_e, ec='green', fc='green', lw=3)
Mars = plt.Circle((1, 3), radius=r_m, ec='brown', fc='brown', lw=3)
ax.add_patch(Sun)
def init():
    ax.add_patch(Earth)
    ax.add_patch(Mars)
    return  Earth, Mars,
def animate(i):
    theta = radians(i)
    ex = d_e*np.cos(theta) - d_e*np.sin(theta)
    ey = d_e*np.sin(theta) + d_e*np.cos(theta)
    mx = d_m*np.cos(theta) - d_m*np.sin(theta)
    my = d_m*np.sin(theta) + d_m*np.cos(theta)
    Earth.center = (ex, ey)
    Earth._angle = i
    Mars.center = (mx, my)
    Mars._angle = i
    return Earth, Mars,
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=1080, 
                               interval=25, blit=True)

plt.show()

最新更新