49

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

Hannes Ovrén
  • 21,229
  • 9
  • 65
  • 75
Vineet
  • 1,492
  • 4
  • 17
  • 31

2 Answers2

107

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
Hannes Ovrén
  • 21,229
  • 9
  • 65
  • 75
  • 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 Apr 11 '18 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. – Ben Jackson Nov 13 '18 at 22:27
  • I had the same issue as you, @Raksha you can take a look here: https://medium.com/lambda-school-machine-learning/making-animations-work-in-google-colaboratory-new-home-for-ml-prototyping-c6147186ae75 and here https://stackoverflow.com/questions/41602588/matplotlib-3d-scatter-animations – vhcandido Feb 15 '19 at 18:52
  • 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 Mar 28 '19 at 10:03
  • @FreyaW Good point! I've updated the answer to move the `%matplotlib` magic to the top. – Hannes Ovrén Jan 22 '20 at 12:06
  • 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 Jun 01 '20 at 21:06
  • 1
    Running the exact same code as OP with %matplotlib notebook at the top in Jupyter Notebook and all I get is a blank screen at the output. – Lucas Baldo Aug 03 '20 at 16:32
  • 4
    This doesn't seem to work when running jupyter in `vscode` – Jules G.M. Nov 01 '21 at 21:06
  • @JulesG.M. I tried it in VS Code, too. It behaves definitely as you describe it. When I try it in a browser, however, with %matplotlib notebook at the very beginning, I get: Javascript Error: IPython is not defined – Stanislav Koncebovski Nov 08 '21 at 12:14
  • @JulesG.M., @stanislav-koncebovski, my experience is that which matplotlib backend you should use depends on which versions of matplotlib and jupyter you are currently using. E.g. I sometimes use the `widget` backend instead of `notebook`, especially when running in JupyterLab. I guess running it in vscode might give the same type of problems. Making sure you run the latest versions of everything is probably a good idea also. – Hannes Ovrén Dec 17 '21 at 07:57
  • `%matplotlib widget` works best in VS code. – nalyd88 Jul 07 '23 at 11:46
  • @nalyd88 Thanks, I've updated the answer. However, it seems like `ipympl` is the preferred choice these days. Worked fine in VS Code for me. – Hannes Ovrén Jul 13 '23 at 07:49
-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)
alexchet
  • 479
  • 1
  • 5
  • 14