我正在使用更新绘图功能使用Matplotlib对火星和地球轨道进行仿真,但是我试图绘制一条连接两者的直线,以便它可以描述火星的逆行运动。 如何在_update_plot中添加plt.plot()函数?
def _update_plot(i, fig, scat):
scat.set_offsets(([math.cos(math.radians(i))*5, math.sin(math.radians(i))*5], [math.cos(math.radians(i/2))*10, math.sin(math.radians(i/2))*10], [0, 0]))
print('Frames: %d' %i)
return scat*
fig = plt.figure()
x = [0]
y = [0]
ax = fig.add_subplot(111)
ax.grid(True, linestyle = '-', color = '0.10')
ax.set_xlim([-50, 50])
ax.set_ylim([-50, 50])
scat = plt.scatter(x, y, c = x)
scat.set_alpha(0.8)
anim = animation.FuncAnimation(fig, _update_plot, fargs = (fig, scat),
frames = 720, interval = 10)
plt.show()
答案 0 :(得分:0)
这是您的主意吗?
import math
def _update_plot(i, fig, scat, l):
scat.set_offsets(([math.cos(math.radians(i))*5, math.sin(math.radians(i))*5], [math.cos(math.radians(i/2))*10, math.sin(math.radians(i/2))*10], [0, 0]))
l.set_data(([math.cos(math.radians(i))*5,math.cos(math.radians(i/2))*10],[math.sin(math.radians(i))*5,math.sin(math.radians(i/2))*10]))
return [scat,l]
fig = plt.figure()
x = [0]
y = [0]
ax = fig.add_subplot(111)
ax.set_aspect('equal')
ax.grid(True, linestyle = '-', color = '0.10')
ax.set_xlim([-11, 11])
ax.set_ylim([-11, 11])
l, = plt.plot([],[], 'r--', zorder=1)
scat = plt.scatter(x, y, c = x, zorder=2)
scat.set_alpha(0.8)
anim = animation.FuncAnimation(fig, _update_plot, fargs = (fig, scat, l),
frames = 720, interval = 10)
plt.show()