Adrien
Adrien

Reputation: 15

Animation not running in Jupiter notebook

I have some code that runs in Spyder, however when I try to run it in Jupiter notebook it doesn't work (I've tried putting %matplot lib at the top of the cell but I just get the following error: AttributeError: 'NoneType' object has no attribute 'lower') This is the code:

import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import animation
def randrange(n, vmin, vmax):
   return (vmax - vmin) * np.random.rand(n) + vmin
n = 100
xx = randrange(n, 23, 32)
yy = randrange(n, 0, 100)
zz = randrange(n, -50, -25)
fig = plt.figure()
ax = Axes3D(fig)


def init():
    ax.scatter(xx, yy, zz, marker='o', s=20, c="goldenrod", alpha=0.6)
    return fig,

def animate(i):
    ax.view_init(elev=10., azim=i)
    return fig,

# Animate
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=360, interval=20, blit=True)

Any ideas?

Upvotes: 0

Views: 525

Answers (1)

r-beginners
r-beginners

Reputation: 35205

The Jupiter environment does not have the ability to render animations, so it seems to use the HTML video function. I am jupyterLab and I always run with this.

import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import animation
from IPython.display import HTML # updated

def randrange(n, vmin, vmax):
    return (vmax - vmin) * np.random.rand(n) + vmin

n = 100
xx = randrange(n, 23, 32)
yy = randrange(n, 0, 100)
zz = randrange(n, -50, -25)
fig = plt.figure()
ax = Axes3D(fig)


def init():
    ax.scatter(xx, yy, zz, marker='o', s=20, c="goldenrod", alpha=0.6)
    return fig,

def animate(i):
    ax.view_init(elev=10., azim=i)
    return fig,

# Animate
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=360, interval=20, blit=True)

plt.close() # updated
HTML(anim.to_html5_video()) # updated

Upvotes: 1

Related Questions