我想将这个轨道行星的2D动画变成3D动画,以便在3D中可视化轨迹,如果可能的话,在轨迹上添加一条轨迹:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
fig = plt.figure()
fig.set_dpi(100)
fig.set_size_inches(7, 6.5)
ax = plt.axes(xlim=(-10, 10), ylim=(-10, 10))
patch1 = plt.Circle((1, -1), 0.1, fc='b')
patch2 = plt.Circle((2, -2), 0.1, fc='r')
patch3 = plt.Circle((3, -3), 0.1, fc='r')
def init():
patch1.center = (0, 0)
patch2.center = (0, 0)
patch3.center = (0, 0)
ax.add_patch(patch1)
ax.add_patch(patch2)
ax.add_patch(patch3)
return patch1, patch2, patch3,
def animate(i):
x, y = patch1.center
a, b = patch2.center
u, v = patch3.center
x = 0 + 3 * np.sin(np.radians(i))
y = 0 + 3 * np.cos(np.radians(i))
a = 0 + 1.5 * np.sin(np.radians(2*i))
b = 0 + 1.5 * np.cos(np.radians(2*i))
u = 0 + 5 * np.sin(np.radians(i/2))
v = 0 + 5 * np.cos(np.radians(i/2))
patch1.center = (x, y)
patch2.center = (a, b)
patch3.center = (u, v)
return patch1, patch2, patch3,
anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=100000,
interval=20,
blit=True)
plt.show()
我试过了:
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_xlim3d(-10,10);
ax.set_ylim3d(-10, 10);
ax.set_zlim3d(-10, 10);
再向其余代码添加第三个坐标,但不显示任何内容......