# 5 Best Ways to Display Rotatable 3D Plots in IPython or Jupyter Notebook

Rate this post

π‘ Problem Formulation: Data visualization is crucial in data science, particularly when dealing with multi-dimensional datasets. A common need arises to visualize 3D data in an interactive manner, allowing for rotation and exploration of complex structures. This article addresses the problem of displaying rotatable 3D plots within IPython or Jupyter Notebook. The goal is to provide methods that enable the user to not only render 3D plots but also interact with them to gain better insights into their data.

## Method 1: Using matplotlib with the mplot3d Toolkit

The `mplot3d` toolkit is an extension of `matplotlib` that allows the creation of 3D plots. It provides an easy-to-use interface for creating static or interactive plots directly within Jupyter notebooks. Using this method, you can produce a variety of 3D plot types including scatter, surface, wireframe, and contour plots that are rotatable through mouse interactions.

Here’s an example:

```%matplotlib notebook
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()

# Create data
x = np.random.standard_normal(100)
y = np.random.standard_normal(100)
z = np.random.standard_normal(100)

# Plot data
ax.scatter(x, y, z)

plt.show()```

Upon running the code, a rotatable 3D scatter plot appears.

This method allows users to plot 3D data directly onto the notebook. By calling `%matplotlib notebook`, it activates the interactive plot mode in Jupyter, which enables rotation and zooming of the resulting scatter plot. The `Axes3D` object is utilized to create a 3-dimensional axes, and the `scatter` function plots the random data points in space, creating a dynamic exploration experience.

## Method 2: Using Plotly

Plotly is a graphing library that makes interactive, publication-quality graphs online. It can create 3D plots that are inherently interactive, like rotating, zooming, and hovering to display data values. Plotly’s 3D plots are rendered using WebGL, which makes them suitable for large datasets and complex interactions.

Here’s an example:

```import plotly.graph_objs as go
import plotly.offline as py
import numpy as np

# Generate example data
x, y, z = np.random.multivariate_normal([0, 0, 0], [[1, 0, 0], [0, 1, 0], [0, 0, 1]], 100).T

# Create the plot
trace = go.Scatter3d(x=x, y=y, z=z, mode='markers')
fig = go.Figure(data=[trace])

# Render the plot
py.iplot(fig)```

The result is an interactive 3D scatter plot within the Jupyter Notebook environment.

Plotly’s `Scatter3d` function creates a 3D scatter plot with ease. It requires x, y, and z coordinates to place the points in a three-dimensional space. The `py.iplot` function then renders the plot within the notebook, offering a highly interactive and user-friendly visualization with features like zooming and rotating right out of the box.

## Method 3: Using Mayavi

Mayavi is a 3D scientific data visualization and plotting in Python. It uses powerful rendering features that give it high-quality output and it can be a bit more complex to use than matplotlib or Plotly. Mayavi integrates with Jupyter notebooks through the `ipyvolume` widget which can then display interactive 3D visualization.

Here’s an example:

```from mayavi import mlab
mlab.init_notebook()
import numpy as np

x, y, z = np.random.random((3, 100))

mlab.points3d(x, y, z, colormap="copper", scale_factor=0.1)```

The output is an interactive 3D plot within the Jupyter Notebook.

This code snippet uses Mayavi’s `points3d` function to create a 3D scatter plot. The `init_notebook` method is necessary to prepare the Jupyter Notebook environment for interactive 3D visualizations. Mayavi provides advanced rendering capabilities, which can be very useful for scientific visualizations requiring high-quality output.

## Method 4: Using IPyVolume

IPyVolume is a Python library for 3D plotting in Jupyter notebooks, which makes heavy use of IPython widgets to provide an interactive experience. It can render large volumes of data and create animations, and is tailored towards scientific visualization of large 3D data sets.

Here’s an example:

```import ipyvolume as ipv
import numpy as np

# Create some 3D data
x, y, z = np.random.random((3, 1000))

# Create a plot and display it
ipv.figure()
ipv.scatter(x, y, z)
ipv.show()```

An interactive 3D scatter plot is displayed, which can be rotated and zoomed.

IPyVolume’s `scatter` function creates a 3D scatter plot in a straightforward manner. The `figure` function initializes a new plot, to which data points are added via the `scatter` method. Finally, the `show` function displays the plot. IPyVolume is particularly strong for its smooth animations and interactive capabilities that can handle large 3D datasets intuitively.

## Bonus One-Liner Method 5: matplotlib “3d” Projection

While technically part of Method 1, you can create a rotatable 3D plot with just a one-liner change in an existing `matplotlib` plot code by adding a `projection='3d'` argument to your axis.

Here’s an example:

```import matplotlib.pyplot as plt

ax = plt.axes(projection='3d')
# Data for a three-dimensional line
zline = np.linspace(0, 15, 1000)
xline = np.sin(zline)
yline = np.cos(zline)
ax.plot3D(xline, yline, zline, 'gray')```

A simple 3D line plot appears in the notebook, ready to be rotated.

By modifying the `axes` function to include the `projection='3d'` argument, `matplotlib` creates a 3D space for plotting. This method is incredibly simple for transforming existing `matplotlib` plots to their 3D counterparts.

## Summary/Discussion

• Method 1: matplotlib with mplot3d. Integrates seamlessly with Jupyter Notebooks. Good for basic 3D plotting needs. Might be limited in rendering performance for very large datasets.
• Method 2: Plotly. Creates highly interactive and aesthetically pleasing plots. Suitable for web presentation. May require online setup or additional dependencies.
• Method 3: Mayavi. Offers high-quality 3D visualizations. Great for scientific computing. More complex setup and limited by Python 2 or special dependencies in Python 3.
• Method 4: IPyVolume. Optimized for large datasets and smooth interactions. Allows 3D volume rendering and animations. Might have a steeper learning curve for those unfamiliar with IPython widgets.
• Bonus Method 5: matplotlib “3d” Projection. Quick and easy approach to get 3D plots. Limited to functionalities of `matplotlib` and might not handle large datasets as efficiently as other methods.