我有这个包含3D图的代码。我在Spyder中运行代码;我想知道是否有可能使这个绘图成为一个旋转的(360度)并保存它。谢谢!附:对不起,如果这是一个愚蠢的问题,但我是Python的新手。
import matplotlib.pyplot as plt
import numpy as np
from scipy import array
jet = plt.get_cmap('jet')
from matplotlib import animation
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.linspace(70,40,4)
Y = np.linspace(5,2,4)
X,Y= np.meshgrid(X, Y)
Z = array ([
[1223.539555, 1428.075086,1714.479425, 2144.053223],
[1567.26647,1829.056119,2990.416079,2745.320067],
[2135.163957,2491.534201, 2990.416079,3738.761638],
[3257.280827, 3800.655101, 4561.372117, 5702.458776],
])
surf = ax.plot_surface(X, Y, Z, rstride = 1, cstride = 1, cmap = jet,linewidth = 0,alpha= 1)
ax.set_zlim3d(0, Z.max())
fig.colorbar(surf, shrink=0.8, aspect=5)
ax.set_xlabel('Axial Length [mm]')
ax.set_ylabel('nbTurns')
ax.set_zlabel('RPM')
plt.show()
您需要定义一个函数才能获得特定的动画。在您的情况下,它是一个简单的轮换:
def rotate(angle):
ax.view_init(azim=angle)
然后使用matplotlib动画:
rot_animation = animation.FuncAnimation(fig, rotate, frames=np.arange(0,362,2),interval=100)
这将调用旋转函数,其中frames
参数为角度并且间隔为100ms
,因此这将导致360°
上的旋转,每个2°
使用100ms
步。要将动画保存为gif文件:
rot_animation.save('path/rotation.gif', dpi=80, writer='imagemagick')