Subplots features¶
Automatic layout¶
By default, ProPlot automatically determines the suitable figure width and height for your subplot grid. The figure dimensions are constrained by the dimensions of the reference subplot. In practice, this usually sets the properties for all subplots, unless your subplots have different aspect ratios or your subplot grid is very complex.
This feature is controlled by a variety of subplots
keyword arguments.
To set the reference subplot, use
ref
(default is1
, i.e. the subplot in the upper left corner). To set the refernce subplot aspect ratio, useaspect
(default is1
).If you use either
axwidth
oraxheight
, both figure dimensions are determined automatically and the reference subplot aspect ratio is preserved (default isaxwidth=2
).If you use either
width
orheight
, the other figure dimension is determined automatically and the reference subplot aspect ratio is preserved.If you use both
width
andheight
(orfigsize
, which is a(width, height)
tuple), the figure dimensions are fixed and the reference subplot aspect ratio is not preserved.When the
ref
subplot aspect ratio mode is set to'equal'
, as with Geographic and polar plots andimshow
plots, the existing aspect will be used instead of the inputaspect
.
By default, ProPlot also 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.
Matplotlib has its own tight layout algorithm,
but ProPlot’s algorithm may change the figure dimensions (depending on the keyword arguments you passed to subplots
) and permits variable spacing between subsequent subplot rows and columns, thanks to the new GridSpec
class.
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.
If you pass
left='2em'
tosubplots
, the left margin is fixed but the right, bottom, and top margins are determined automatically.If you pass
wspace=('3em', None)
andncols=3
tosubplots
, the space between the first two columns is fixed, while the space between the second two columns is determined automatically.
The below examples are stress tests of the tight layout algorithm and demonstrate how “reference axes” affect the figure appearence. Note that since ProPlot colorbar and panel widths are specified in physical units, they are held fixed during resizing. This helps us avoid colorbars that look “too skinny” or “too fat”.
[1]:
import proplot as plot
# Same plot with different reference axes
for ref in (1, 2):
f, axs = plot.subplots(
ref=ref, nrows=3, ncols=3, wratios=(3, 2, 2),
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 simple grids'
)
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']
)
/home/docs/checkouts/readthedocs.org/user_builds/proplot/conda/v0.4.1/lib/python3.7/site-packages/proplot/utils.py:103: ProPlotWarning: Rebuilding font manager.
[2]:
import proplot as plot
# Same plot with different reference axes
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(
title='reference axes', titleweight='bold',
titleloc='uc', titlecolor='red9'
)
axs.format(xlabel='xlabel', ylabel='ylabel', suptitle='Super title')
axs[0].format(xlabel='xlabel\nxlabel\nxlabel')
axs[1].format(
ylabel='ylabel\nylabel\nylabel', ytickloc='both',
yticklabelloc='both', ctitle='Title'
)
axs[2].format(
yformatter='null', ctitle='Title',
ytickloc='both', yticklabelloc='both'
)
axs[3].format(yformatter='null', xlabel='xlabel\nxlabel\nxlabel')
axs.format(
suptitle='Tight layout with complex grids',
rowlabels=['Row 1', 'Row 2'],
collabels=['Column 1', 'Column 2']
)
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.
[3]:
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='13cm', height='2in',
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
.
[4]:
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='Flush subplot grid'
)