Subplots features¶
Automatic figure sizing¶
By default, ProPlot automatically determines the suitable figure size given the geometry of your subplot grid. The figure size is constrained by the physical size of a reference subplot. This algorithm is controlled by a variety of subplots
keyword arguments:
ref
sets the reference subplot number (default is1
, i.e. the subplot in the upper left corner).aspect
sets the reference subplot aspect ratio (default is1
). You can also use matplotlib’sset_aspect
.axwidth
andaxheight
set the physical dimensions of the reference subplot (default isaxwidth=2
). If one is specified, the other is calculated to satisfyaspect
. If both are specified,aspect
is ignored. The physical dimensions of the figure are determined automatically.width
andheight
set the physical dimensions of the figure. If one is specified, the other is calculated to satisfyaspect
and the subplot spacing. If both are specified (or if the matplotlibfigsize
parameter is specified),aspect
is ignored.
This algorithm also includes the following notable features:
For very simple subplot grids (e.g.
plot.subplots(ncols=2, nrows=3)
),aspect
,axwidth
, andaxheight
apply to every subplot in the figure – not just the reference subplot.When the reference subplot aspect ratio has been manually overridden (e.g. with
ax.set_aspect(1)
) or is set to'equal'
(as with map projections andimshow
images), theaspect
parameter is ignored.When
colorbar
s andpanel
s are present in the figure, their physical widths are preserved during figure resizing. ProPlot specifies their widths in physical units to help avoid colorbars that look “too skinny” or “too fat”.
The below examples demonstrate the default behavior of the automatic figure sizing algorithm, and how it can be controlled with subplots
keyword arguments.
[1]:
import proplot as plot
import numpy as np
# Cartopy projections
f, axs = plot.subplots(ncols=2, nrows=3, proj='robin')
axs.format(
land=True, landcolor='k',
suptitle='Auto figure sizing with grid of cartopy projections'
)
# Images
state = np.random.RandomState(51423)
f, axs = plot.subplots(ncols=2, nrows=3)
colors = state.rand(10, 20, 3).cumsum(axis=2)
colors /= colors.max()
axs.imshow(colors)
axs.format(
land=True, landcolor='k',
suptitle='Auto figure sizing with grid of images'
)
/home/docs/checkouts/readthedocs.org/user_builds/proplot/conda/v0.5.0/lib/python3.7/site-packages/proplot/utils.py:105: ProPlotWarning: Rebuilding font cache.
[2]:
import proplot as plot
# Loop through different axes widths
suptitle = 'Effect of subplot properties on figure size'
for axwidth in ('4cm', '6cm'):
f, axs = plot.subplots(ncols=2, axwidth=axwidth,)
axs[0].format(
suptitle=suptitle,
title=f'axwidth = {axwidth}', titleweight='bold',
titleloc='uc', titlecolor='red9',
)
# Loop through different aspect ratios
for aspect in (1, (3,2)):
f, axs = plot.subplots(ncols=2, nrows=2, axwidth=1.6, aspect=aspect)
axs[0].format(
suptitle=suptitle,
title=f'aspect = {aspect}', titleweight='bold',
titleloc='uc', titlecolor='red9',
)
[3]:
import proplot as plot
# Changing the reference subplot in the presence of unequal width/height ratios
suptitle = 'Effect of reference subplot on figure size'
for ref in (1, 2):
f, axs = plot.subplots(
ref=ref, nrows=3, ncols=3, wratios=(3, 2, 2),
axwidth=1.1,
)
axs[ref-1].format(
suptitle=suptitle,
title='reference axes', titleweight='bold',
titleloc='uc', titlecolor='red9'
)
# Changing the reference subplot in a complex grid
for ref in (3, 2):
f, axs = plot.subplots(
[[1, 1, 2], [3, 4, 4]],
hratios=(1, 1.5), wratios=(3, 2, 2),
ref=ref, axwidth=1.1, span=False
)
axs[ref-1].format(
suptitle=suptitle,
title='reference axes', titleweight='bold',
titleloc='uc', titlecolor='red9'
)
Automatic subplot spacing¶
By default, ProPlot applies a tight layout algorithm to every figure. This algorithm automatically adjusts the space between subplot rows and columns and the figure edge to accomadate labels. It can be disabled by passing tight=False
to subplots
.
While matplotlib has its own tight layout algorithm, ProPlot’s algorithm permits variable spacing between subsequent subplot rows and columns (see the new GridSpec
class) and may change the figure size (depending on the keyword arguments passed to subplots
).
ProPlot’s tight layout algorithm can also be easily overridden. When you pass a spacing argument like left
, right
, top
, bottom
, wspace
, or hspace
to subplots
, that value is always respected:
With
left='2em'
, the left margin is fixed but the right, bottom, and top margins are calculated automatically.With
wspace=('3em', None)
(andncols=3
), the space between the first two columns is fixed, while the space between the second two columns is calculated automatically.
The below examples demonstrate how the tight layout algorithm permits variable spacing between subplot rows and columns.
[4]:
import proplot as plot
f, axs = plot.subplots(
ref=ref, nrows=3, ncols=3, axwidth=1.1, share=0
)
axs[ref-1].format(
title='reference axes', titleweight='bold',
titleloc='uc', titlecolor='red9'
)
axs[4].format(
title='title\ntitle\ntitle',
suptitle='Tight layout with variable row-column spacing'
)
axs[1].format(ylabel='ylabel\nylabel\nylabel')
axs[:4:2].format(xlabel='xlabel\nxlabel\nxlabel')
axs.format(
rowlabels=['Row 1', 'Row 2', 'Row 3'],
collabels=['Column 1', 'Column 2', 'Column 3']
)
[5]:
import proplot as plot
f, axs = plot.subplots(
ncols=4, nrows=3, wspace=(0, 0, None), hspace=(0, None),
bottom='5em', right='5em', span=False,
axwidth=1.1,
)
axs.format(
suptitle='Tight layout with user overrides',
rowlabels=['Row 1', 'Row 2', 'Row 3'],
collabels=['Column 1', 'Column 2', 'Column 3', 'Column 4']
)
axs[0, :].format(xtickloc='top')
axs[2, :].format(xtickloc='both')
axs[:, 1].format(ytickloc='neither')
axs[:, 2].format(ytickloc='right')
axs[:, 3].format(ytickloc='both')
axs[-1, :].format(title='Title\nTitle\nTitle', xlabel='xlabel')
axs[:, 0].format(ylabel='ylabel\nylabel')
Arbitrary physical units¶
ProPlot supports arbitrary physical units for controlling the figure width
and height
; the reference subplot axwidth
and axheight
; the gridspec spacing values left
, right
, bottom
, top
, wspace
, and hspace
; and in a few other places, e.g. panel
and colorbar
widths.
If a sizing argument is numeric, the units are inches or points, and if string, the units are interpreted by units
. A table of acceptable units is found in the units
documentation. They include centimeters, millimeters, pixels, em-heights, and points.
[6]:
import proplot as plot
import numpy as np
with plot.rc.context(small='12px', large='15px', linewidth='0.5mm'):
f, axs = plot.subplots(
ncols=3, width='15cm', height='2.5in',
wspace=('10pt', '20pt'), right='10mm'
)
panel = axs[2].panel_axes('r', width='2em')
axs.format(
suptitle='Arguments with arbitrary units',
xlabel='x axis', ylabel='y axis'
)
Subplot numbers and labels¶
ProPlot assigns unique numbers to subplots. The number order determines the order the subplots appear in the subplot_grid
and the order of “a-b-c” labels generated by format
. If you did not provide a 2D array, the number order is row-major by default but can be made column-major by passing order='F'
to subplots
. The “a-b-c” label position and style can be changed with format
.
[7]:
import proplot as plot
f, axs = plot.subplots(nrows=8, ncols=8, axwidth=0.7, space=0)
axs.format(
abc=True, abcloc='ur', xlabel='x axis', ylabel='y axis',
xticks=[], yticks=[], suptitle='Subplot labels demo'
)