Skip to content

Commit 3def76f

Browse files
committed
Add sharex/y, spanx/y, etc. setters and getters, add ref prop
1 parent b114e61 commit 3def76f

File tree

2 files changed

+135
-22
lines changed

2 files changed

+135
-22
lines changed

proplot/axes.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,11 @@ def _sharex_setup(self, sharex, level=None):
453453
level = self.figure._sharex
454454
if level not in range(4):
455455
raise ValueError(
456+
'Invalid sharing level sharex={value!r}. '
456457
'Axis sharing level can be 0 (share nothing), '
457458
'1 (hide axis labels), '
458459
'2 (share limits and hide axis labels), or '
459-
'3 (share limits and hide axis and tick labels). Got {level}.'
460+
'3 (share limits and hide axis and tick labels).'
460461
)
461462
self._share_short_axis(sharex, 'l', level)
462463
self._share_short_axis(sharex, 'r', level)
@@ -470,10 +471,11 @@ def _sharey_setup(self, sharey, level=None):
470471
level = self.figure._sharey
471472
if level not in range(4):
472473
raise ValueError(
474+
'Invalid sharing level sharey={value!r}. '
473475
'Axis sharing level can be 0 (share nothing), '
474476
'1 (hide axis labels), '
475477
'2 (share limits and hide axis labels), or '
476-
'3 (share limits and hide axis and tick labels). Got {level}.'
478+
'3 (share limits and hide axis and tick labels).'
477479
)
478480
self._share_short_axis(sharey, 'b', level)
479481
self._share_short_axis(sharey, 't', level)

proplot/subplots.py

Lines changed: 131 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import matplotlib.figure as mfigure
1414
import matplotlib.transforms as mtransforms
1515
import matplotlib.gridspec as mgridspec
16+
from numbers import Integral
1617
from .rctools import rc
1718
from .utils import _warn_proplot, _notNone, _counter, _setstate, units
1819
from . import projs, axes
@@ -1642,16 +1643,57 @@ def colorbar(
16421643
*args, **kwargs
16431644
Passed to `~proplot.axes.Axes.colorbar`.
16441645
"""
1645-
if 'cax' in kwargs:
1646-
return super().colorbar(*args, **kwargs)
1647-
elif 'ax' in kwargs:
1648-
return kwargs.pop('ax').colorbar(
1649-
*args, space=space, width=width, **kwargs)
1650-
else:
1651-
ax = self._add_figure_panel(
1652-
loc, space=space, width=width, span=span,
1653-
row=row, col=col, rows=rows, cols=cols)
1654-
return ax.colorbar(*args, loc='_fill', **kwargs)
1646+
ax = kwargs.pop('ax', None)
1647+
cax = kwargs.pop('cax', None)
1648+
# Fill this axes
1649+
if cax is not None:
1650+
return super().colorbar(*args, cax=cax, **kwargs)
1651+
# Generate axes panel
1652+
elif ax is not None:
1653+
return ax.colorbar(*args, space=space, width=width, **kwargs)
1654+
# Generate figure panel
1655+
ax = self._add_figure_panel(
1656+
loc, space=space, width=width, span=span,
1657+
row=row, col=col, rows=rows, cols=cols
1658+
)
1659+
return ax.colorbar(*args, loc='_fill', **kwargs)
1660+
1661+
def get_alignx(self):
1662+
"""Return the *x* axis label alignment mode."""
1663+
return self._alignx
1664+
1665+
def get_aligny(self):
1666+
"""Return the *y* axis label alignment mode."""
1667+
return self._aligny
1668+
1669+
def get_gridspec(self):
1670+
"""Return the single `GridSpec` instance associated with this figure.
1671+
If the `GridSpec` has not yet been initialized, returns ``None``."""
1672+
return self._gridspec
1673+
1674+
def get_ref_axes(self):
1675+
"""Return the reference axes associated with the reference axes
1676+
number `Figure.ref`."""
1677+
for ax in self._mainaxes:
1678+
if ax.number == self.ref:
1679+
return ax
1680+
return None # no error
1681+
1682+
def get_sharex(self):
1683+
"""Return the *x* axis sharing level."""
1684+
return self._sharex
1685+
1686+
def get_sharey(self):
1687+
"""Return the *y* axis sharing level."""
1688+
return self._sharey
1689+
1690+
def get_spanx(self):
1691+
"""Return the *x* axis label spanning mode."""
1692+
return self._spanx
1693+
1694+
def get_spany(self):
1695+
"""Return the *y* axis label spanning mode."""
1696+
return self._spany
16551697

16561698
def draw(self, renderer):
16571699
# Certain backends *still* have issues with the tight layout
@@ -1715,16 +1757,16 @@ def legend(
17151757
*args, **kwargs
17161758
Passed to `~proplot.axes.Axes.legend`.
17171759
"""
1718-
if 'ax' in kwargs:
1719-
return kwargs.pop('ax').legend(
1720-
*args, space=space, width=width, **kwargs
1721-
)
1722-
else:
1723-
ax = self._add_figure_panel(
1724-
loc, space=space, width=width, span=span,
1725-
row=row, col=col, rows=rows, cols=cols
1726-
)
1727-
return ax.legend(*args, loc='_fill', **kwargs)
1760+
ax = kwargs.pop('ax', None)
1761+
# Generate axes panel
1762+
if ax is not None:
1763+
return ax.legend(*args, space=space, width=width, **kwargs)
1764+
# Generate figure panel
1765+
ax = self._add_figure_panel(
1766+
loc, space=space, width=width, span=span,
1767+
row=row, col=col, rows=rows, cols=cols
1768+
)
1769+
return ax.legend(*args, loc='_fill', **kwargs)
17281770

17291771
def save(self, filename, **kwargs):
17301772
# Alias for `~Figure.savefig` because ``fig.savefig`` is redundant.
@@ -1753,6 +1795,75 @@ def set_canvas(self, canvas):
17531795
canvas.print_figure = _canvas_preprocess(canvas, 'print_figure')
17541796
super().set_canvas(canvas)
17551797

1798+
def set_alignx(self, value):
1799+
"""Set the *x* axis label alignment mode."""
1800+
self.stale = True
1801+
self._alignx = bool(value)
1802+
1803+
def set_aligny(self, value):
1804+
"""Set the *y* axis label alignment mode."""
1805+
self.stale = True
1806+
self._aligny = bool(value)
1807+
1808+
def set_sharex(self, value):
1809+
"""Set the *x* axis sharing level."""
1810+
value = int(value)
1811+
if value not in range(4):
1812+
raise ValueError(
1813+
'Invalid sharing level sharex={value!r}. '
1814+
'Axis sharing level can be 0 (share nothing), '
1815+
'1 (hide axis labels), '
1816+
'2 (share limits and hide axis labels), or '
1817+
'3 (share limits and hide axis and tick labels).'
1818+
)
1819+
self.stale = True
1820+
self._sharex = value
1821+
1822+
def set_sharey(self, value):
1823+
"""Set the *y* axis sharing level."""
1824+
value = int(value)
1825+
if value not in range(4):
1826+
raise ValueError(
1827+
'Invalid sharing level sharey={value!r}. '
1828+
'Axis sharing level can be 0 (share nothing), '
1829+
'1 (hide axis labels), '
1830+
'2 (share limits and hide axis labels), or '
1831+
'3 (share limits and hide axis and tick labels).'
1832+
)
1833+
self.stale = True
1834+
self._sharey = value
1835+
1836+
def set_spanx(self, value):
1837+
"""Set the *x* axis label spanning mode."""
1838+
self.stale = True
1839+
self._spanx = bool(value)
1840+
1841+
def set_spany(self, value):
1842+
"""Set the *y* axis label spanning mode."""
1843+
self.stale = True
1844+
self._spany = bool(value)
1845+
1846+
@property
1847+
def gridspec(self):
1848+
"""The single `GridSpec` instance used for all subplots
1849+
in the figure."""
1850+
return self._gridspec_main
1851+
1852+
@property
1853+
def ref(self):
1854+
"""The reference axes number. The `axwidth`, `axheight`, and `aspect`
1855+
`subplots` and `figure` arguments are applied to this axes, and aspect
1856+
ratio is conserved for this axes in tight layout adjustment."""
1857+
return self._ref
1858+
1859+
@ref.setter
1860+
def ref(self, ref):
1861+
if not isinstance(ref, Integral) or ref < 1:
1862+
raise ValueError(
1863+
f'Invalid axes number {ref!r}. Must be integer >=1.')
1864+
self.stale = True
1865+
self._ref = ref
1866+
17561867
def set_size_inches(self, w, h=None, forward=True, auto=False):
17571868
# Set the figure size and, if this is being called manually or from
17581869
# an interactive backend, override the geometry tracker so users can

0 commit comments

Comments
 (0)