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

时间:2017-11-22 05:59:09

标签: python matplotlib

情节只显示白屏,我不完全确定原因。我希望行星在不同位置围绕太阳运行,在那里,我使用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()

1 个答案:

答案 0 :(得分:0)

我正在玩你的代码。主要问题是(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()