Reputation: 3
I am trying to create a 3D line animation, on top of a 3D surface plot in matplotlib.
I am able to plot the 3D surface, but there is no animation. And there is no error in the code. I am setting the X,Y and Z values of the 3D line upto the current frame.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import animation
def f(x,y):
return x+y
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X = np.arange(0, 10, 1)
Y = np.arange(0, 10, 1)
Z = X+Y
X1, Y1 = np.meshgrid(X, Y)
Z1 = f(X1, Y1)
ax.plot_surface(X1, Y1, Z1, color='b', alpha=0.5)
plt.show()
line, = ax.plot([], [], [], lw=2)
def init():
line.set_data([], [])
line.set_3d_properties([])
return line
def animate(i, line, X, Y, Z):
line.set_data(X[:i], Y[:i])
line.set_3d_properties(Z[:i])
return line
anim = animation.FuncAnimation(fig, animate, init_func=init, fargs=(line, X, Y, Z),
frames=10, interval=200,
repeat_delay=5, blit=True)
plt.show()
Upvotes: 0
Views: 2894
Reputation: 339052
plt.show()
before any animation is even defined. Remove the first plt.show()
.You will then get errors as expected. The problem is that you need to return a list of artists from the animating functions when using blit=True
. This is easily achieved by adding a comma,
return line,
Complete code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib import animation
def f(x,y):
return x+y
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
X = np.arange(0, 10, 1)
Y = np.arange(0, 10, 1)
Z = X+Y
X1, Y1 = np.meshgrid(X, Y)
Z1 = f(X1, Y1)
ax.plot_surface(X1, Y1, Z1, color='red', alpha=0.5)
line, = ax.plot([], [], [], lw=2)
def init():
line.set_data([], [])
line.set_3d_properties([])
return line,
def animate(i, line, X, Y, Z):
line.set_data(X[:i], Y[:i])
line.set_3d_properties(Z[:i])
return line,
anim = animation.FuncAnimation(fig, animate, init_func=init, fargs=(line, X, Y, Z),
frames=10, interval=200,
repeat_delay=5, blit=True)
plt.show()
Upvotes: 1