Chapter 35. Three-Dimensional Plotting in Matplotlib
Matplotlib was initially designed with only two-dimensional plotting in
mind. Around the time of the 1.0 release, some three-dimensional
plotting utilities were built on top of Matplotlib’s
two-dimensional display, and the result is a convenient (if somewhat
limited) set of tools for three-dimensional data visualization.
Three-dimensional plots are enabled by importing the mplot3d toolkit,
included with the main Matplotlib installation:
In[1]:frommpl_toolkitsimportmplot3d
Once this submodule is imported, a three-dimensional axes can be created
by passing the keyword projection='3d' to any
of the normal axes creation routines, as shown here (see Figure 35-1).
In[2]:%matplotlibinlineimportnumpyasnpimportmatplotlib.pyplotasplt
In[3]:fig=plt.figure()ax=plt.axes(projection='3d')
With this three-dimensional axes enabled, we can now plot a variety of
three-dimensional plot types. Three-dimensional plotting is one of the
functionalities that benefits immensely from viewing figures
interactively rather than statically, in the notebook; recall that to
use interactive figures, you can use %matplotlib notebook rather than
%matplotlib inline when running this code.
Figure 35-1. An empty three-dimensional axes
Three-Dimensional Points and Lines
The most basic three-dimensional plot is a line or collection of scatter ...