44

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
  • 20,169
  • 8
  • 66
  • 74
Vineet
  • 1,318
  • 2
  • 15
  • 26

2 Answers2

88

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 = ...
Hannes Ovrén
  • 20,169
  • 8
  • 66
  • 74
  • 13
    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
  • 3
    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
  • 2
    @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
  • 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
  • 3
    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
-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
  • 459
  • 5
  • 14