Note
Click here to download the full example code
Generating 3D plots using the mplot3d toolkit.
Contents
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')
New in version 1.0.0: This approach is the preferred method of creating a 3D axes.
Note
Prior to version 1.0.0, the method of creating a 3D axes was
different. For those using older versions of matplotlib, change
ax = fig.add_subplot(111, projection='3d')
to ax = Axes3D(fig)
.
See the mplot3d FAQ for more information about the mplot3d toolkit.
Axes3D.
plot
(xs, ys, *args, zdir='z', **kwargs)[source]¶Plot 2D or 3D data.
Parameters: | xs : 1D array-like
ys : 1D array-like
zs : scalar or 1D array-like
zdir : {'x', 'y', 'z'}
**kwargs
|
---|
Axes3D.
scatter
(xs, ys, zs=0, zdir='z', s=20, c=None, depthshade=True, *args, **kwargs)[source]¶Create a scatter plot.
Parameters: | xs, ys : array-like
zs : float or array-like, optional, default: 0
zdir : {'x', 'y', 'z', '-x', '-y', '-z'}, optional, default: 'z'
s : scalar or array-like, optional, default: 20
c : color, sequence, or sequence of color, optional
depthshade : bool, optional, default: True
**kwargs
|
---|---|
Returns: | paths : |
Axes3D.
plot_wireframe
(X, Y, Z, *args, **kwargs)[source]¶Plot a 3D wireframe.
Note
The rcount and ccount kwargs, which both default to 50, determine the maximum number of samples used in each direction. If the input data is larger, it will be downsampled (by slicing) to these numbers of points.
Parameters: | X, Y, Z : 2d arrays
rcount, ccount : int
rstride, cstride : int
**kwargs
|
---|
Axes3D.
plot_surface
(X, Y, Z, *args, norm=None, vmin=None, vmax=None, lightsource=None, **kwargs)[source]¶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.
Note
The rcount and ccount kwargs, which both default to 50, determine the maximum number of samples used in each direction. If the input data is larger, it will be downsampled (by slicing) to these numbers of points.
Parameters: | X, Y, Z : 2d arrays
rcount, ccount : int
rstride, cstride : int
color : color-like
cmap : Colormap
facecolors : array-like of colors.
norm : Normalize
vmin, vmax : float
shade : bool
lightsource :
**kwargs
|
---|
Axes3D.
plot_trisurf
(*args, color=None, norm=None, vmin=None, vmax=None, lightsource=None, **kwargs)[source]¶Plot a triangulated surface.
The (optional) triangulation can be specified in one of two ways; either:
plot_trisurf(triangulation, ...)
where triangulation is a Triangulation
object, or:
plot_trisurf(X, Y, ...)
plot_trisurf(X, Y, triangles, ...)
plot_trisurf(X, Y, triangles=triangles, ...)
in which case a Triangulation object will be created. See
Triangulation
for a explanation of
these possibilities.
The remaining arguments are:
plot_trisurf(..., Z)
where Z is the array of values to contour, one per point in the triangulation.
Parameters: | X, Y, Z : array-like
color
cmap
norm : Normalize
vmin, vmax : scalar, optional, default: None
shade : bool
lightsource :
**kwargs
|
---|
Examples
(Source code, png, pdf)
(Source code, png, pdf)
New in version 1.2.0: This plotting function was added for the v1.2.0 release.
Axes3D.
contour
(X, Y, Z, *args, extend3d=False, stride=5, zdir='z', offset=None, **kwargs)[source]¶Create a 3D contour plot.
Parameters: | X, Y, Z : array-likes
extend3d : bool
stride : int
zdir : {'x', 'y', 'z'}
offset : scalar
*args, **kwargs
|
---|---|
Returns: | matplotlib.contour.QuadContourSet |
Axes3D.
contourf
(X, Y, Z, *args, zdir='z', offset=None, **kwargs)[source]¶Create a 3D filled contour plot.
Parameters: | X, Y, Z : array-likes
zdir : {'x', 'y', 'z'}
offset : scalar
*args, **kwargs
|
---|---|
Returns: | matplotlib.contour.QuadContourSet |
Notes
New in version 1.1.0: The zdir and offset parameters.
New in version 1.1.0: The feature demoed in the second contourf3d example was enabled as a result of a bugfix for version 1.1.0.
Axes3D.
bar
(left, height, zs=0, zdir='z', *args, **kwargs)[source]¶Add 2D bar(s).
Parameters: | left : 1D array-like
height : 1D array-like
zs : scalar or 1D array-like
zdir : {'x', 'y', 'z'}
**kwargs
|
---|---|
Returns: | mpl_toolkits.mplot3d.art3d.Patch3DCollection |
Axes3D.
quiver
(X, Y, Z, U, V, W, /, length=1, arrow_length_ratio=.3, pivot='tail', normalize=False, **kwargs)[source]¶Plot a 3D field of arrows.
The arguments could be array-like or scalars, so long as they they can be broadcast together. The arguments can also be masked arrays. If an element in any of argument is masked, then that corresponding quiver element will not be plotted.
Parameters: | X, Y, Z : array-like
U, V, W : array-like
length : float
arrow_length_ratio : float
pivot : {'tail', 'middle', 'tip'}
normalize : bool
**kwargs
|
---|
Having multiple 3D plots in a single figure is the same as it is for 2D plots. Also, you can have both 2D and 3D plots in the same figure.
New in version 1.0.0: Subplotting 3D plots was added in v1.0.0. Earlier version can not do this.
Traceback (most recent call last):
File "/build/matplotlib-mO9dyQ/matplotlib-3.1.2/tutorials/toolkits/mplot3d.py", line 1
===================
^
SyntaxError: invalid syntax
===================
The mplot3d Toolkit
===================
Generating 3D plots using the mplot3d toolkit.
.. currentmodule:: mpl_toolkits.mplot3d
.. contents::
:backlinks: none
.. _toolkit_mplot3d-tutorial:
Getting started
---------------
An Axes3D object is created just like any other axes using
the projection='3d' keyword.
Create a new :class:`matplotlib.figure.Figure` and
add a new axes to it of type :class:`~mpl_toolkits.mplot3d.Axes3D`::
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
.. versionadded:: 1.0.0
This approach is the preferred method of creating a 3D axes.
.. note::
Prior to version 1.0.0, the method of creating a 3D axes was
different. For those using older versions of matplotlib, change
``ax = fig.add_subplot(111, projection='3d')``
to ``ax = Axes3D(fig)``.
See the :ref:`toolkit_mplot3d-faq` for more information about the mplot3d
toolkit.
.. _plot3d:
Line plots
====================
.. automethod:: Axes3D.plot
.. figure:: ../../gallery/mplot3d/images/sphx_glr_lines3d_001.png
:target: ../../gallery/mplot3d/lines3d.html
:align: center
:scale: 50
Lines3d
.. _scatter3d:
Scatter plots
=============
.. automethod:: Axes3D.scatter
.. figure:: ../../gallery/mplot3d/images/sphx_glr_scatter3d_001.png
:target: ../../gallery/mplot3d/scatter3d.html
:align: center
:scale: 50
Scatter3d
.. _wireframe:
Wireframe plots
===============
.. automethod:: Axes3D.plot_wireframe
.. figure:: ../../gallery/mplot3d/images/sphx_glr_wire3d_001.png
:target: ../../gallery/mplot3d/wire3d.html
:align: center
:scale: 50
Wire3d
.. _surface:
Surface plots
=============
.. automethod:: Axes3D.plot_surface
.. figure:: ../../gallery/mplot3d/images/sphx_glr_surface3d_001.png
:target: ../../gallery/mplot3d/surface3d.html
:align: center
:scale: 50
Surface3d
Surface3d 2
Surface3d 3
.. _trisurface:
Tri-Surface plots
=================
.. automethod:: Axes3D.plot_trisurf
.. figure:: ../../gallery/mplot3d/images/sphx_glr_trisurf3d_001.png
:target: ../../gallery/mplot3d/trisurf3d.html
:align: center
:scale: 50
Trisurf3d
.. _contour3d:
Contour plots
=============
.. automethod:: Axes3D.contour
.. figure:: ../../gallery/mplot3d/images/sphx_glr_contour3d_001.png
:target: ../../gallery/mplot3d/contour3d.html
:align: center
:scale: 50
Contour3d
Contour3d 2
Contour3d 3
.. _contourf3d:
Filled contour plots
====================
.. automethod:: Axes3D.contourf
.. figure:: ../../gallery/mplot3d/images/sphx_glr_contourf3d_001.png
:target: ../../gallery/mplot3d/contourf3d.html
:align: center
:scale: 50
Contourf3d
Contourf3d 2
.. versionadded:: 1.1.0
The feature demoed in the second contourf3d example was enabled as a
result of a bugfix for version 1.1.0.
.. _polygon3d:
Polygon plots
====================
.. automethod:: Axes3D.add_collection3d
.. figure:: ../../gallery/mplot3d/images/sphx_glr_polys3d_001.png
:target: ../../gallery/mplot3d/polys3d.html
:align: center
:scale: 50
Polys3d
.. _bar3d:
Bar plots
====================
.. automethod:: Axes3D.bar
.. figure:: ../../gallery/mplot3d/images/sphx_glr_bars3d_001.png
:target: ../../gallery/mplot3d/bars3d.html
:align: center
:scale: 50
Bars3d
.. _quiver3d:
Quiver
====================
.. automethod:: Axes3D.quiver
.. figure:: ../../gallery/mplot3d/images/sphx_glr_quiver3d_001.png
:target: ../../gallery/mplot3d/quiver3d.html
:align: center
:scale: 50
Quiver3d
.. _2dcollections3d:
2D plots in 3D
====================
.. figure:: ../../gallery/mplot3d/images/sphx_glr_2dcollections3d_001.png
:target: ../../gallery/mplot3d/2dcollections3d.html
:align: center
:scale: 50
2dcollections3d
.. _text3d:
Text
====================
.. automethod:: Axes3D.text
.. figure:: ../../gallery/mplot3d/images/sphx_glr_text3d_001.png
:target: ../../gallery/mplot3d/text3d.html
:align: center
:scale: 50
Text3d
.. _3dsubplots:
Subplotting
====================
Having multiple 3D plots in a single figure is the same
as it is for 2D plots. Also, you can have both 2D and 3D plots
in the same figure.
.. versionadded:: 1.0.0
Subplotting 3D plots was added in v1.0.0. Earlier version can not
do this.
.. figure:: ../../gallery/mplot3d/images/sphx_glr_subplot3d_001.png
:target: ../../gallery/mplot3d/subplot3d.html
:align: center
:scale: 50
Subplot3d
Mixed Subplots
"""
Keywords: matplotlib code example, codex, python plot, pyplot Gallery generated by Sphinx-Gallery