Skip to content

Commit 04280d7

Browse files
committed
Move sharex/y, spanx/y, alignx/y from subplots() to Figure()
1 parent 3def76f commit 04280d7

File tree

1 file changed

+69
-69
lines changed

1 file changed

+69
-69
lines changed

proplot/subplots.py

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -802,11 +802,12 @@ class Figure(mfigure.Figure):
802802

803803
def __init__(
804804
self, tight=None,
805-
ref=1, pad=None, axpad=None, panelpad=None,
806-
includepanels=False,
807-
autoformat=True,
805+
ref=1, pad=None, axpad=None, panelpad=None, includepanels=False,
806+
span=None, spanx=None, spany=None,
807+
align=None, alignx=None, aligny=None,
808+
share=None, sharex=None, sharey=None,
809+
autoformat=True, fallback_to_cm=None,
808810
gridspec_kw=None, subplots_kw=None, subplots_orig_kw=None,
809-
fallback_to_cm=None,
810811
**kwargs
811812
):
812813
"""
@@ -836,6 +837,36 @@ def __init__(
836837
Whether to include panels when centering *x* axis labels,
837838
*y* axis labels, and figure "super titles" along the edge of the
838839
subplot grid. Default is ``False``.
840+
sharex, sharey, share : {3, 2, 1, 0}, optional
841+
The "axis sharing level" for the *x* axis, *y* axis, or both axes.
842+
Default is ``3``. This can considerably reduce redundancy in your
843+
figure. Options are as follows.
844+
845+
0. No axis sharing. Also sets the default `spanx` and `spany`
846+
values to ``False``.
847+
1. Only draw *axis label* on the leftmost column (*y*) or
848+
bottommost row (*x*) of subplots. Axis tick labels
849+
still appear on every subplot.
850+
2. As in 1, but forces the axis limits to be identical. Axis
851+
tick labels still appear on every subplot.
852+
3. As in 2, but only show the *axis tick labels* on the
853+
leftmost column (*y*) or bottommost row (*x*) of subplots.
854+
855+
spanx, spany, span : bool or {0, 1}, optional
856+
Toggles "spanning" axis labels for the *x* axis, *y* axis, or both
857+
axes. Default is ``False`` if `sharex`, `sharey`, or `share` are
858+
``0``, ``True`` otherwise. When ``True``, a single, centered axis
859+
label is used for all axes with bottom and left edges in the same
860+
row or column. This can considerably redundancy in your figure.
861+
862+
"Spanning" labels integrate with "shared" axes. For example,
863+
for a 3-row, 3-column figure, with ``sharey > 1`` and ``spany=1``,
864+
your figure will have 1 ylabel instead of 9.
865+
alignx, aligny, align : bool or {0, 1}, optional
866+
Default is ``False``. Whether to `align axis labels \
867+
<https://matplotlib.org/3.1.1/gallery/subplots_axes_and_figures/align_labels_demo.html>`__
868+
for the *x* axis, *y* axis, or both axes. Only has an effect when
869+
`spanx`, `spany`, or `span` are ``False``.
839870
autoformat : bool, optional
840871
Whether to automatically configure *x* axis labels, *y* axis
841872
labels, axis formatters, axes titles, colorbar labels, and legend
@@ -860,9 +891,7 @@ def __init__(
860891
See also
861892
--------
862893
`~matplotlib.figure.Figure`
863-
"""
864-
# Initialize first, because need to provide fully initialized figure
865-
# as argument to gridspec, because matplotlib tight_layout does that
894+
""" # noqa
866895
tight_layout = kwargs.pop('tight_layout', None)
867896
constrained_layout = kwargs.pop('constrained_layout', None)
868897
if tight_layout or constrained_layout:
@@ -872,10 +901,36 @@ def __init__(
872901
'own tight layout algorithm, activated by default or with '
873902
'tight=True.'
874903
)
904+
905+
# Initialize first, because need to provide fully initialized figure
906+
# as argument to gridspec, because matplotlib tight_layout does that
875907
self._authorized_add_subplot = False
876908
self._is_preprocessing = False
877909
self._is_resizing = False
878910
super().__init__(**kwargs)
911+
912+
# Axes sharing and spanning settings
913+
sharex = _notNone(sharex, share, rc['share'])
914+
sharey = _notNone(sharey, share, rc['share'])
915+
spanx = _notNone(spanx, span, 0 if sharex == 0 else None, rc['span'])
916+
spany = _notNone(spany, span, 0 if sharey == 0 else None, rc['span'])
917+
if spanx and (alignx or align):
918+
_warn_proplot(f'"alignx" has no effect when spanx=True.')
919+
if spany and (aligny or align):
920+
_warn_proplot(f'"aligny" has no effect when spany=True.')
921+
alignx = _notNone(alignx, align, rc['align'])
922+
aligny = _notNone(aligny, align, rc['align'])
923+
self.set_alignx(alignx)
924+
self.set_aligny(aligny)
925+
self.set_sharex(sharex)
926+
self.set_sharey(sharey)
927+
self.set_spanx(spanx)
928+
self.set_spany(spany)
929+
930+
# Various other attributes
931+
gridspec_kw = gridspec_kw or {}
932+
gridspec = GridSpec(self, **gridspec_kw)
933+
nrows, ncols = gridspec.get_active_geometry()
879934
self._pad = units(_notNone(pad, rc['subplots.pad']))
880935
self._axpad = units(_notNone(axpad, rc['subplots.axpad']))
881936
self._panelpad = units(_notNone(panelpad, rc['subplots.panelpad']))
@@ -891,8 +946,6 @@ def __init__(
891946
self._tpanels = []
892947
self._lpanels = []
893948
self._rpanels = []
894-
gridspec = GridSpec(self, **(gridspec_kw or {}))
895-
nrows, ncols = gridspec.get_active_geometry()
896949
self._barray = np.empty((0, ncols), dtype=bool)
897950
self._tarray = np.empty((0, ncols), dtype=bool)
898951
self._larray = np.empty((0, nrows), dtype=bool)
@@ -2092,36 +2145,6 @@ def subplots(
20922145
subplots and the figure edge. Units are interpreted by
20932146
`~proplot.utils.units`. By default, these are determined by the
20942147
"tight layout" algorithm.
2095-
2096-
sharex, sharey, share : {3, 2, 1, 0}, optional
2097-
The "axis sharing level" for the *x* axis, *y* axis, or both axes.
2098-
Default is ``3``. The options are as follows:
2099-
2100-
0. No axis sharing. Also sets the default `spanx` and `spany` values
2101-
to ``False``.
2102-
1. Only draw *axis label* on the leftmost column (*y*) or
2103-
bottommost row (*x*) of subplots. Axis tick labels
2104-
still appear on every subplot.
2105-
2. As in 1, but forces the axis limits to be identical. Axis
2106-
tick labels still appear on every subplot.
2107-
3. As in 2, but only show the *axis tick labels* on the
2108-
leftmost column (*y*) or bottommost row (*x*) of subplots.
2109-
2110-
spanx, spany, span : bool or {1, 0}, optional
2111-
Toggles "spanning" axis labels for the *x* axis, *y* axis, or both
2112-
axes. Default is ``False`` if `sharex`, `sharey`, or `share` are ``0``,
2113-
``True`` otherwise. When ``True``, a single, centered axis label
2114-
is used for all axes with bottom and left edges in the same row or
2115-
column.
2116-
2117-
Note that "spanning" labels are integrated with "shared" axes. For
2118-
example, for a 3-row, 3-column figure, with ``sharey > 1`` and
2119-
``spany=1``, your figure will have 1 ylabel instead of 9.
2120-
alignx, aligny, align : bool or {0, 1}, optional
2121-
Whether to `align axis labels \
2122-
<https://matplotlib.org/3.1.1/gallery/subplots_axes_and_figures/align_labels_demo.html>`__
2123-
for the *x* axis, *y* axis, or both axes. Only has an effect when
2124-
`spanx`, `spany`, or `span` are ``False``. Default is ``False``.
21252148
proj, projection : str or dict-like, optional
21262149
The map projection name. The argument is interpreted as follows.
21272150
@@ -2207,36 +2230,12 @@ def subplots(
22072230
)
22082231
nrows, ncols = array.shape
22092232

2210-
# Figure out rows and columns "spanned" by each axes in list, for
2211-
# axis sharing and axis label spanning settings
2212-
sharex = int(_notNone(sharex, share, rc['share']))
2213-
sharey = int(_notNone(sharey, share, rc['share']))
2214-
if sharex not in range(4) or sharey not in range(4):
2215-
raise ValueError(
2216-
f'Axis sharing level can be 0 (no sharing), '
2217-
'1 (sharing, but keep all tick labels), '
2218-
'2 (sharing, keep one set of tick labels), '
2219-
'or 3 (sharing, keep one axis label and one set of tick labels)'
2220-
'Got sharex={sharex} and sharey={sharey}.'
2221-
)
2222-
spanx = _notNone(spanx, span, 0 if sharex == 0 else None, rc['span'])
2223-
spany = _notNone(spany, span, 0 if sharey == 0 else None, rc['span'])
2224-
alignx = _notNone(alignx, align)
2225-
aligny = _notNone(aligny, align)
2226-
if (spanx and alignx) or (spany and aligny):
2227-
_warn_proplot(
2228-
f'The "alignx" and "aligny" args have no effect when '
2229-
'"spanx" and "spany" are True.'
2230-
)
2231-
alignx = _notNone(alignx, rc['align'])
2232-
aligny = _notNone(alignx, rc['align'])
22332233
# Get some axes properties, where locations are sorted by axes id.
22342234
# NOTE: These ranges are endpoint exclusive, like a slice object!
22352235
axids = [np.where(array == i) for i in np.sort(
22362236
np.unique(array)) if i > 0] # 0 stands for empty
22372237
xrange = np.array([[x.min(), x.max()] for _, x in axids])
2238-
yrange = np.array([[y.min(), y.max()]
2239-
for y, _ in axids]) # range accounting for panels
2238+
yrange = np.array([[y.min(), y.max()] for y, _ in axids])
22402239
xref = xrange[ref - 1, :] # range for reference axes
22412240
yref = yrange[ref - 1, :]
22422241

@@ -2345,6 +2344,9 @@ def subplots(
23452344
}
23462345

23472346
# Apply default spaces
2347+
share = kwargs.get('share', None)
2348+
sharex = _notNone(kwargs.get('sharex', None), share, rc['share'])
2349+
sharey = _notNone(kwargs.get('sharey', None), share, rc['share'])
23482350
left = _notNone(left, _get_space('left'))
23492351
right = _notNone(right, _get_space('right'))
23502352
bottom = _notNone(bottom, _get_space('bottom'))
@@ -2383,11 +2385,9 @@ def subplots(
23832385
subplotspec = gridspec[y0:y1 + 1, x0:x1 + 1]
23842386
with fig._authorize_add_subplot():
23852387
axs[idx] = fig.add_subplot(
2386-
subplotspec, number=num,
2387-
spanx=spanx, spany=spany, alignx=alignx, aligny=aligny,
2388-
sharex=sharex, sharey=sharey,
2389-
main=True,
2390-
**axes_kw[num])
2388+
subplotspec, number=num, main=True,
2389+
**axes_kw[num]
2390+
)
23912391

23922392
# Shared axes setup
23932393
# TODO: Figure out how to defer this to drawtime in #50

0 commit comments

Comments
 (0)