51

I am not sure about how to rotate graph in Python Jupyter notebook, its static for me and not rotate on mouse movement

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

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

x =[1,2,3,4,5,6,7,8,9,10]
y =[5,6,2,3,13,4,1,2,4,8]
z =[2,3,3,3,5,7,9,11,9,10]

ax.scatter(x, y, z, c='r', marker='o')

ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()

enter image description here

0

2 Answers 2

108

To enable interactivity you need to use the notebook backend of matplotlib. You can do this by running %matplotlib notebook.

This must be done before you plot anything, e.g.:

%matplotlib notebook

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

fig = ...

Update 2023-07-13

This answer is getting old and the choice of backends have changed over time (I used the widget backend for many years).

The current best choice for interactive plots in Jupyter Notebooks is supposedly the ipympl backend. You need to install it and then use

%matplotlib ipympl
13
  • 17
    when i add that to my Colab notebook, it doesn't show the animation at all :( ... even if I remove the line, I have to restart the kernel for the stationary graph to show up
    – Raksha
    Commented Apr 11, 2018 at 16:33
  • 4
    Note that this is is an alternative to something like %matplotlib inline, so don't try to combine them. The effects of matplotlib.rcParams['figure.figsize'] do not quite match so you will want to adjust those as well. Commented Nov 13, 2018 at 22:27
  • 4
    If someone else has the same problem as @Raksha, you need to put %matplotlib notebook at the very top of your code, before importing your modules. And restart your kernels. Maybe the answer yould be edited to reflect that more general solution.
    – Freya W
    Commented Mar 28, 2019 at 10:03
  • 3
    @HannesOvrén Even after adding the line %matplotlib notebook instead of %matplotlib inline and restarting the kernel, I'm still not able to have an interactive 3D plot that could rotate! Is there anything else that needs to be done?
    – Aman Singh
    Commented Jun 1, 2020 at 21:06
  • 4
    This doesn't seem to work when running jupyter in vscode Commented Nov 1, 2021 at 21:06
-2

As described on matplotlib website you can create an interactive graph by importing mplot3d. Please use the following sample Rotate Axes.

I am going to include the code just in case the link is not available in future.

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

%matplotlib notebook

fig = plt.figure()
ax = fig.add_subplot(111, 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)
3
  • 2
    this does not work in OP context (he clearly states he is using a Jupyter Notebook)
    – Manuel G
    Commented Dec 24, 2017 at 17:10
  • @ManuelG Granted it doesn't offer interactivity (With the code update now it does). But the code runs on a Jupyter Notebook, the link also has an example.
    – alexchet
    Commented Dec 26, 2017 at 14:02
  • this doesn't work for me on Google Colab :(
    – Raksha
    Commented Apr 11, 2018 at 16:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.