Edoardo
Edoardo

Reputation: 23

Python: How to use matplotlib.animation?

I'm trying to use matplotlib.animation in order to animate a surface, but I can't really understand the documentation, anyway this is my code:

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)

And I get as a result this error:

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

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

So here's my questions:

Thanks

Upvotes: 2

Views: 242

Answers (1)

edyvedy13
edyvedy13

Reputation: 2296

Try to remove the blit arguement,

If blit=True, func and init_func should return an iterable of drawables to clear.

You're returning a single Axes3D, not an iterable of drawables.

Upvotes: 2

Related Questions