PCeltide
PCeltide

Reputation: 105

Matplotlib Animation not rendering

I am trying to render an animation of rotating a 3d graph, but the animation is not working, even if I try to use the code given here: https://matplotlib.org/3.5.0/gallery/mplot3d/rotate_axes3d_sgskip.html

The output I get is one static image enter image description here and <Figure size 432x288 with 0 Axes> repeated multipe times over and over again.

I am using anaconda, jupyter notebooks and also tried using google colab... what is going wrong and how can I fix this? Thank you very much!

Upvotes: 0

Views: 2213

Answers (3)

JB97
JB97

Reputation: 51

I ran into a similar issue before, replacing "%matplotlib inline" with "%matplotlib notebook" in the given code solved it for me.

Upvotes: 1

Uchiha012
Uchiha012

Reputation: 851

Add %matplotlib notebook in the cell of Jupyter notebook for an interactive backend. Execute the below cell, it should work

%matplotlib notebook
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

# load some test data for demonstration and plot a wireframe
X, Y, Z = axes3d.get_test_data(0.1)
ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5)

# rotate the axes and update
for angle in range(0, 360):
    ax.view_init(30, angle)
    plt.draw()
    plt.pause(.001)

Upvotes: 1

Davide_sd
Davide_sd

Reputation: 13185

Since you are using Jupyter Notebook, you need to install ipympl and execute the following command on a cell at the top of your notebook: %matplotlib widget. This will enable an interactive frame which is going to wrap the matplotlib figure in the output cell.

Then, you need to use matplotlib's FuncAnimation:

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

fig = plt.figure()
ax = fig.add_subplot(projection='3d')

# load some test data for demonstration and plot a wireframe
X, Y, Z = axes3d.get_test_data(0.1)
ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5)

def animate(angle):
    ax.view_init(30, angle)

ani = FuncAnimation(fig, animate, frames=range(360))

Upvotes: 1

Related Questions