3D Plotting in Python

Sebastian Norena
4 min readApr 25, 2018

--

There are many options for doing 3D plots in python, here I will explain some of the more comon using Matplotlib. In general the first step is to create a 3D axes, and then plot any of the 3D graphs that best ilustrates the data for a particular need.

In order to use Matplotlib, the mplot3d toolkit that is included with the Matplotlib installation has to be imported:

from mpl_toolkits import mplot3d

Then, to create a 3D axes you can execute this code:

%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
fig = plt.figure()
ax = plt.axes(projection=’3d’)
3D Axes

It is inside this 3D axes that a plot can be drawn, it is important to know what type of plot (or combination of plots) will be better to describe the data.

Points and Lines:

The following image combines 2 plots, one with a line that goes through every point of the data, and other that draws a point on each of the particular 1000 values on this example.

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’)
# Data for three-dimensional scattered points
zdata = 15 * np.random.random(100)
xdata = np.sin(zdata) + 0.1 * np.random.randn(100)
ydata = np.cos(zdata) + 0.1 * np.random.randn(100)
ax.scatter3D(xdata, ydata, zdata, c=zdata, cmap=’Greens’);
Points and Lines, 3D trigonometric spiral

3D Contour Plots:

The input for the contour plot is a bit different than for the previous plots, as it needs the data on a two dimmensional grid, note on the following example that after assigning values for x and y, they are combined on a grid by executing “np.meshgrid(x, y)” and then the Z values are created from executing the function f(X,Y) with the values of the grid (Z=f(X,Y)).

def f(x, y):
return np.sin(np.sqrt(x ** 2 + y ** 2))

x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, 50, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');
Contour Plot, 3D sinusoidal function

Wireframes:

This type of graph also works with data on a grid, notice it takes the variables X, Y, Z we created above which are a grid.

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_wireframe(X, Y, Z, color='black')
Wireframe

Surface Plot:

Similar to a Wireframe but has a filling on its faces.

ax = plt.axes(projection=’3d’)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1,cmap=’viridis’,edgecolor=’none’)
Surface

On the previous graphs, the data was generated in order, but in real life sometimes the data is not ordered, for those cases the surface triangulation is very useful as it creates surfaces by finding sets of triangles formed between adjacent points.

Surface Triangulation:

theta = 2 * np.pi * np.random.random(1000)
r = 6 * np.random.random(1000)
x = np.ravel(r * np.sin(theta))
y = np.ravel(r * np.cos(theta))
z = f(x, y)
ax = plt.axes(projection=’3d’)
ax.plot_trisurf(x, y, z,cmap=’viridis’, edgecolor=’none’);

This publication has some content from the Python Data Science Handbook by Jake VanderPlas; Jupyter notebooks are available on GitHub.

The text is released under the CC-BY-NC-ND license, and code is released under the MIT license. Some of the content was consulted on this book

--

--