matplotlib

Table Of Contents

This Page

mplot3d tutorial

Getting started

An Axes3D object is created just like any other axes using the projection=‘3d’ keyword. Create a new matplotlib.figure.Figure and add a new axes to it of type Axes3D:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

Line plots

Axes3D.plot(xs, ys, *args, **kwargs)

Plot 2D or 3D data.

Argument Description
xs, ys X, y coordinates of vertices
zs z value(s), either one for all points or one for each point.
zdir Which direction to use as z (‘x’, ‘y’ or ‘z’) when plotting a 2d set.

Other arguments are passed on to plot()

[source code, hires.png, pdf]

../../_images/lines3d_demo.png

Scatter plots

Axes3D.scatter(xs, ys, zs=0, zdir='z', *args, **kwargs)

Create a scatter plot.

Argument Description
xs, ys Positions of data points.
zs Either an array of the same length as xs and ys or a single value to place all points in the same plane. Default is 0.
zdir Which direction to use as z (‘x’, ‘y’ or ‘z’) when plotting a 2d set.

Keyword arguments are passed on to scatter().

Returns a Patch3DCollection

[source code, hires.png, pdf]

../../_images/scatter3d_demo.png

Wireframe plots

Axes3D.plot_wireframe(X, Y, Z, *args, **kwargs)

Plot a 3D wireframe.

Argument Description
X, Y, Data values as numpy.arrays
Z  
rstride Array row stride (step size)
cstride Array column stride (step size)

Keyword arguments are passed on to matplotlib.collections.LineCollection.__init__().

Returns a Line3DCollection

[source code, hires.png, pdf]

../../_images/wire3d_demo.png

Surface plots

Axes3D.plot_surface(X, Y, Z, *args, **kwargs)

Create a surface plot.

By default it will be colored in shades of a solid color, but it also supports color mapping by supplying the cmap argument.

Argument Description
X, Y, Z Data values as numpy.arrays
rstride Array row stride (step size)
cstride Array column stride (step size)
color Color of the surface patches
cmap A colormap for the surface patches.
facecolors Face colors for the individual patches
norm An instance of Normalize to map values to colors
vmin Minimum value to map
vmax Maximum value to map
shade Whether to shade the facecolors

Other arguments are passed on to __init__()

[source code, hires.png, pdf]

../../_images/surface3d_demo.png

[source code, hires.png, pdf]

../../_images/surface3d_demo2.png

[source code, hires.png, pdf]

../../_images/surface3d_demo3.png

Contour plots

Axes3D.contour(X, Y, Z, *args, **kwargs)

Create a 3D contour plot.

Argument Description
X, Y, Data values as numpy.arrays
Z  
extend3d Whether to extend contour in 3D (default: False)
stride Stride (step size) for extending contour
zdir The direction to use: x, y or z (default)
offset If specified plot a projection of the contour lines on this position in plane normal to zdir

The positional and other keyword arguments are passed on to contour()

Returns a contour

[source code, hires.png, pdf]

../../_images/contour3d_demo.png

[source code, hires.png, pdf]

../../_images/contour3d_demo2.png

[source code, hires.png, pdf]

../../_images/contour3d_demo3.png

Filled contour plots

Axes3D.contourf(X, Y, Z, *args, **kwargs)

Plot filled 3D contours.

X, Y, Z: data points.

The positional and keyword arguments are passed on to contourf()

Returns a contourf

[source code, hires.png, pdf]

../../_images/contourf3d_demo.png

Polygon plots

Axes3D.add_collection3d(col, zs=0, zdir='z')

Add a 3d collection object to the plot.

2D collection types are converted to a 3D version by modifying the object and adding z coordinate information.

Supported are:
  • PolyCollection
  • LineColleciton
  • PatchCollection

[source code, hires.png, pdf]

../../_images/polys3d_demo.png

Bar plots

Axes3D.bar(left, height, zs=0, zdir='z', *args, **kwargs)

Add 2D bar(s).

Argument Description
left The x coordinates of the left sides of the bars.
height The height of the bars.
zs Z coordinate of bars, if one value is specified they will all be placed at the same z.
zdir Which direction to use as z (‘x’, ‘y’ or ‘z’) when plotting a 2d set.

Keyword arguments are passed onto bar().

Returns a Patch3DCollection

[source code, hires.png, pdf]

../../_images/bars3d_demo.png

2D plots in 3D

[source code, hires.png, pdf]

../../_images/2dcollections3d_demo.png

Text

Axes3D.text(x, y, z, s, zdir=None, **kwargs)

Add text to the plot. kwargs will be passed on to Axes.text, except for the zdir keyword, which sets the direction to be used as the z direction.

[source code, hires.png, pdf]

../../_images/text3d_demo.png

Subplotting

Having multiple 3D plots in a single figure is the same as it is for 2D plots. And you can mix 2D and 3D plots into the same figure.

[source code, hires.png, pdf]

../../_images/subplot3d_demo.png

[source code, hires.png, pdf]

../../_images/mixed_subplots_demo.png