数据下载地址 123 KB
z np.load( ./data.npy )转换数据
x轴和y轴的数据。
x np.linspace(0, 31, 32) x, y np.meshgrid(x, y)定义数据更新数据
函数的三个参数
动画要更新的次数 帧 可以当作是数据的索引更新的数据画布def update_map(num, z, plot): plot[0].remove() plot[0] ax.plot_surface(x, y, z[num], cmap cm.Oranges, linewidth 0)
然后定义画布和一些参数
fig, ax plt.subplots(tight_layout True, figsize (6, 6), subplot_kw dict(projection 3d )) plot [ax.plot_surface(x, y, z[0], cmap cm.Oranges, linewidth 0)] ax.set_xlim(0, 31) ax.set_ylim(0, 31) ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([]) ax.axis( off ) ax.plot([0, 0], [0, 31], [0, 0], c black ) ax.plot([0, 31], [0, 0], [0, 0], c black ) ax.plot([31, 0], [31, 31], [0, 0], c black ) ax.plot([31, 31], [0, 31], [0, 0], c black ) ax.view_init(50, 120)绘制 3D 曲面动画并显示
ani animation.FuncAnimation( fig, update_map, 30, interval 100, fargs (z, plot), repeat True) plt.show()保存为gif
需要安装一下imagemagick 谷歌/百度安装一下先。
ani.save( teaser .gif , writer imagemagick ,fps 10)完整代码
import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation from matplotlib import cm, rcParams z np.load( ./data.npy ) x np.linspace(0, 31, 32) x, y np.meshgrid(x, y)



