[Matplotlib-users] axes properties

vincent.adrien at gmail.com vincent.adrien at gmail.com
Tue Mar 21 09:57:14 EDT 2017

Dear Jean-Philippe,

(If I correctly understood your question 1.:) `fig.add_subplot` creates an Axes
instance where you can then plot more or less *any* kind of plot that Matplotlib
supports (line, scatter, hist, patch, etc.). *It is not specific to the '3d'
projection.* `fig.add_subplot(1, 1, 1)` (or `fig.add_subplot(111)` if your
prefer a syntax closer to MATLAB) simply means that you want to create a single
(sub)plot area on your figure canvas. If you are confused because in 2D you
could get a line plot simply with `plt.plot([0, 1, 2])`, understand that under
the hood it is only some kind of wrapper for the (OO-)equivalent:
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot([0, 1, 2])
The difference here to get a 3D Axes instance is that you have to precise the
projection when you create it (`fig.add_subplot(1, 1, 1, projection='3d')`).

Besides (if I am correct), please note that “we” tend to promote using an
equivalent (but more OO-)wrapper, namely `plt.subplots` (the final *s* is
important!). In 2D, you can thus write
fig, ax = plt.subplots()  # create a "111-subplot"
ax.plot([0, 1, 2])
and you can pass different **kwargs to it, among which a 'subplot_kw' dictionary
that allows you to precise the projection. For example if you want to get a
"111-subplot" 3d Axes:
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
It is a bit verbose is this simple situation but becomes rather convenient for
grids with several subplots.

About 2. you may want to have a look at the following properties and methods:
- ax.dist
- ax.azim
- ax.elev
- ax.view_init(elev=..., azim=...)

Here is a small script (PNG output is attached) that demonstrate their usage:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def dummy_plot(ax):
    X, Y = np.meshgrid(np.arange(-5, 5, 0.25), np.arange(-5, 5, 0.25))
    Z = np.sin(np.sqrt(X**2 + Y**2))
    ax.plot_surface(X, Y, Z, cmap='coolwarm')
    # Remove all the ticks for eye pleasure
    return ax

# Here is a way to instantiate all the 2x2 subplots at once, without using
# the `fig.add_subplot` method. *axs* is a 2x2-array of Axes instances.
fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(6.4, 4.8),
                        num="Demo_Jean-Philippe_GRIVET_2",  # <- figure label
                        subplot_kw={'projection': '3d'})

ax = dummy_plot(axs[0, 0])
ax.set_title("Forced defaults: dist=10 (a.u.),\nazim=-60 (deg), elev=30 (deg)")
# NB: these values does not seem to be exactly the (1, 1, 1) direction to me.
ax.dist = 10
ax.view_init(azim=-60, elev=30)
# From the docstring of `ax.view_init`:
# Set the elevation and azimuth of the axes. This can be used to rotate
# the axes programatically:
# - 'elev' stores the elevation angle in the z plane.
# - 'azim' stores the azimuth angle in the x,y plane.

ax = dummy_plot(axs[0, 1])
ax.set_title("Doubled 'dist' value")
ax.dist *= 2

# NB: one can also directly set the `ax.azim` and `ax.elev` properties
ax = dummy_plot(axs[1, 0])
ax.set_title("'azim' increased of 45 deg.")
ax.azim += 45

ax = dummy_plot(axs[1, 1])
ax.set_title("'elev' set to 60 deg.")
ax.elev = 60


Hopefully it will help you to plot what you want :).


On 18/03/2017 17:56, Jean-Philippe GRIVET wrote:
> Thank you so much Vincent  (and Amit) for your detailed answers.
> I have two questions remaining.
> 1. All the code examples that I have looked at (including the "getting
> started" example on page 1798 of the user manual for Matplotlib 2.0)
> uses the "add_subplot" metod. Why should a 3d projection be included
> in a subplot and not in a plot ?
> 2. In all the examples, the figure seems to be viewed down the 1,1,1
> direction towards the origin. How can the view point be changed from
> within the program ?
> Thanks again,
> Jean-Philippe

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Demo_Jean-Philippe_GRIVET_2.png
Type: image/png
Size: 128810 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20170321/d8614daf/attachment-0001.png>

More information about the Matplotlib-users mailing list