Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying rotatable 3D plots in IPython or Jupyter Notebook

(Mac OSX 10.10.5)

I can reproduce from the matplotlib website mplot3d the example code for a 3D scatter plot scatter3d_demo.py, however the plot renders as a static image. I can not click on the graph and dynamically rotate to view the 3D plotted data.

I have achieved the static 3D plot using the example code - using (a) ipython from within Terminal, (b) ipython notebook from within terminal, and (c) ipython notebook launched from the Anaconda launcher.

I think I am missing some very basic step as assumed knowledge.

In past learning, plotting has opened a GUI Python App which has a graph viewer. (Solution 2 in code shown below opens this.) Perhaps I need to know the code to export the output graph to that display method? (Yes, use %matplotlib (only) as first line without inline or notebook as shown in comments in code block below.)

As an example in ipython notebook:

    # These lines are comments     # Initial setup from an online python notebook tutorial is below.      # Note the first line "%matplotlib inline" this is how the tutorial has it.     # Two solutions 1. use: "%matplotlib notebook" graphs appear dynamic in the notebook.     #               2. use: "%matplotlib" (only) graphs appear dynamic in separate window.      #    ( 2. is the best solution for detailed graphs/plots. )      %matplotlib inline       import pandas as pd     import numpy as np     import matplotlib as mpl     import matplotlib.pyplot as plt     from mpl_toolkits.mplot3d import Axes3D      pd.set_option('html',False)     pd.set_option('max_columns',30)     pd.set_option('max_rows',10)       # What follows is a copy of the 3D plot example code.     # Data is randomly generated so there is no external data import.      def randrange(n, vmin, vmax):         return (vmax-vmin)*np.random.rand(n) + vmin      fig = plt.figure()     ax = fig.add_subplot(111, projection='3d')     n = 100     for c, m, zl, zh in [('r', 'o', -60, -25), ('b', '^', -30, -5)]:         xs = randrange(n, 23, 50)         ys = randrange(n, 0, 100)         zs = randrange(n, zl, zh)         ax.scatter(xs, ys, zs, c=c, marker=m)      ax.set_xlabel('X Label')     ax.set_ylabel('Y Label')     ax.set_zlabel('Z Label')      plt.show() 

Can someone identify what I am missing?

Looking at Python 3.3.6 documentation, section 25.1perhaps the tkinter package ...

The tkinter package (“Tk interface”) is the standard Python interface to the Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, as well as on Windows systems.

I think though, this relates to development of GUI programs so I am not sure this is relevant. (Correct, this was not needed for the solution.)

like image 804
Cam_Aust Avatar asked Oct 30 '15 12:10

Cam_Aust


People also ask

How do you plot a 3D plot in Jupyter notebook?

To generate an interactive 3D plot first import the necessary packages and create a random dataset. Now using Axes3D(figure) function from the mplot3d library we can generate a required plot directly. Pass the data to the 3D plot and configure the title and labels.

How do you make a rotatable 3D plot in Python?

Plot a 3D wireframe with data test data x, y, and z. To make it rotatable, we can set the elevation and azimuth of the axes in degrees (not radians), using view_init() method. To show the figure, use plt. show() method.


1 Answers

Use %matplotlib notebook instead of %matplotlib inline to get embedded interactive figures in the IPython notebook – this requires recent versions of matplotlib (1.4+) and IPython (3.0+).

like image 139
jakevdp Avatar answered Sep 18 '22 03:09

jakevdp