Python:如何使用matplotlib.animation?

时间:2017-04-16 16:03:35

标签: python matplotlib

我正在尝试使用matplotlib.animation来为表面设置动画,但我无法理解文档,无论如何这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.animation as animation
from matplotlib import cm

dimension=1.5   

X=np.arange(-dimension,dimension,0.01)
Y=np.arange(-dimension,dimension,0.01)
X, Y = np.meshgrid(X,Y)

def drum(t):    #only a test, will be edited in future
    Z=(X**2+Y**2)*np.cos(t*0.01)
    return Z

def update_drum(t):
    ax.clear()
    ax.plot_surface(X, Y, drum(t), cmap=cm.coolwarm,
                   linewidth=0, antialiased=False)
    plt.show

# Attaching 3D axis to the figure
fig = plt.figure()
ax = p3.Axes3D(fig)

# Setting the axes properties
ax.set_xlim3d([-dimension-0.5,0.5+dimension])
ax.set_xlabel('X')

ax.set_ylim3d([-dimension-0.5,0.5+dimension])
ax.set_ylabel('Y')

ax.set_zlim3d([-3.0, 3.0])
ax.set_zlabel('Z')

ax.set_title('Resonant modes')

# Creating the Animation object
ani = animation.FuncAnimation(fig, update_drum, 25,
                                   interval=50, blit=True)

我得到了这个错误:

axes = set(a.axes for a in artists)

"TypeError: 'Axes3D' object is not iterable".

所以这是我的问题:

  • 我应该如何修改FuncAnimation(update_drum)调用的函数?
  • 如何解决错误“Axes3D”对象不可迭代“

谢谢

1 个答案:

答案 0 :(得分:1)

尝试删除blit争论,

numpy

您将返回单个Axes3D,而不是可绘制的可迭代。