Home > Back-end >  Matplotlib Animation not rendering
Matplotlib Animation not rendering

Time:04-09

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: 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!

CodePudding user response:

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

CodePudding user response:

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))

CodePudding user response:

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)
  • Related