Skip to content

Commit 44fb032

Browse files
committed
Cleanup Figure methods and subplots.py docs
1 parent 03a3e37 commit 44fb032

File tree

1 file changed

+57
-76
lines changed

1 file changed

+57
-76
lines changed

proplot/subplots.py

Lines changed: 57 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,6 @@ class Figure(mfigure.Figure):
809809
the space around the figure edge, between subplots, and between
810810
panels is changed to accommodate subplot content. Figure dimensions
811811
may be automatically scaled to preserve subplot aspect ratios."""
812-
813812
def __init__(
814813
self, tight=None,
815814
ref=1, pad=None, axpad=None, panelpad=None, includepanels=False,
@@ -1027,7 +1026,7 @@ def _add_figure_panel(
10271026
self, side, span=None, row=None, col=None, rows=None, cols=None,
10281027
**kwargs
10291028
):
1030-
"""Adds figure panels. Also modifies the panel attribute stored
1029+
"""Add a figure panel. Also modifies the panel attribute stored
10311030
on the figure to include these panels."""
10321031
# Interpret args and enforce sensible keyword args
10331032
s = side[0]
@@ -1127,9 +1126,9 @@ def _add_figure_panel(
11271126
return pax
11281127

11291128
def _adjust_aspect(self):
1130-
"""Adjust average aspect ratio used for gridspec calculations. This
1131-
fixes grids with identically fixed aspect ratios, e.g. identically
1132-
zoomed-in cartopy projections and imshow images."""
1129+
"""Adjust the average aspect ratio used for gridspec calculations.
1130+
This fixes grids with identically fixed aspect ratios, e.g.
1131+
identically zoomed-in cartopy projections and imshow images."""
11331132
# Get aspect ratio
11341133
if not self._axes_main:
11351134
return
@@ -1159,10 +1158,8 @@ def _adjust_aspect(self):
11591158
self._gridspec_main.update(**gridspec_kw)
11601159

11611160
def _adjust_tight_layout(self, renderer, resize=True):
1162-
"""Applies tight layout scaling that permits flexible figure
1163-
dimensions and preserves panel widths and subplot aspect ratios.
1164-
The `renderer` should be a `~matplotlib.backend_bases.RendererBase`
1165-
instance."""
1161+
"""Apply tight layout scaling that permits flexible figure
1162+
dimensions and preserves panel widths and subplot aspect ratios."""
11661163
# Initial stuff
11671164
axs = self._iter_axes()
11681165
subplots_kw = self._subplots_kw
@@ -1447,8 +1444,8 @@ def _context_preprocessing(self):
14471444
return _setstate(self, _is_preprocessing=True)
14481445

14491446
def _get_align_coord(self, side, axs):
1450-
"""Returns figure coordinate for spanning labels and super title. The
1451-
`x` can be ``'x'`` or ``'y'``."""
1447+
"""Return the figure coordinate for spanning labels or super titles.
1448+
The `x` can be ``'x'`` or ``'y'``."""
14521449
# Get position in figure relative coordinates
14531450
s = side[0]
14541451
x = ('y' if s in 'lr' else 'x')
@@ -1472,7 +1469,7 @@ def _get_align_coord(self, side, axs):
14721469
return pos, spanax
14731470

14741471
def _get_align_axes(self, side):
1475-
"""Returns main axes along the left, right, bottom, or top sides
1472+
"""Return the main axes along the left, right, bottom, or top sides
14761473
of the figure."""
14771474
# Initial stuff
14781475
s = side[0]
@@ -1515,10 +1512,9 @@ def _insert_row_column(
15151512
self, side, idx,
15161513
ratio, space, space_orig, figure=False,
15171514
):
1518-
"""Helper function that "overwrites" the main figure gridspec to make
1519-
room for a panel. The `side` is the panel side, the `idx` is the
1520-
slot you want the panel to occupy, and the remaining args are the
1521-
panel widths and spacings."""
1515+
""""Overwrite" the main figure gridspec to make room for a panel. The
1516+
`side` is the panel side, the `idx` is the slot you want the panel
1517+
to occupy, and the remaining args are the panel widths and spacings."""
15221518
# Constants and stuff
15231519
# Insert spaces to the left of right panels or to the right of
15241520
# left panels. And note that since .insert() pushes everything in
@@ -1616,14 +1612,14 @@ def _insert_row_column(
16161612
return gridspec
16171613

16181614
def _update_figtitle(self, title, **kwargs):
1619-
"""Assign figure "super title"."""
1615+
"""Assign the figure "super title" and update settings."""
16201616
if title is not None and self._suptitle.get_text() != title:
16211617
self._suptitle.set_text(title)
16221618
if kwargs:
16231619
self._suptitle.update(kwargs)
16241620

16251621
def _update_labels(self, ax, side, labels, **kwargs):
1626-
"""Assigns side labels, updates label settings."""
1622+
"""Assign the side labels and update settings."""
16271623
s = side[0]
16281624
if s not in 'lrbt':
16291625
raise ValueError(f'Invalid label side {side!r}.')
@@ -1730,19 +1726,6 @@ def get_aligny(self):
17301726
"""Return the *y* axis label alignment mode."""
17311727
return self._aligny
17321728

1733-
def get_gridspec(self):
1734-
"""Return the single `GridSpec` instance associated with this figure.
1735-
If the `GridSpec` has not yet been initialized, returns ``None``."""
1736-
return self._gridspec
1737-
1738-
def get_ref_axes(self):
1739-
"""Return the reference axes associated with the reference axes
1740-
number `Figure.ref`."""
1741-
for ax in self._mainaxes:
1742-
if ax.number == self.ref:
1743-
return ax
1744-
return None # no error
1745-
17461729
def get_sharex(self):
17471730
"""Return the *x* axis sharing level."""
17481731
return self._sharex
@@ -1834,13 +1817,11 @@ def legend(
18341817

18351818
def save(self, filename, **kwargs):
18361819
# Alias for `~Figure.savefig` because ``fig.savefig`` is redundant.
1837-
# Also automatically expands user paths e.g. the tilde ``'~'``.
18381820
return self.savefig(filename, **kwargs)
18391821

18401822
def savefig(self, filename, **kwargs):
1841-
# Automatically expand user because why in gods name does
1842-
# matplotlib not already do this. Undocumented because do not
1843-
# want to overwrite matplotlib docstring.
1823+
# Automatically expand user the user name. Undocumented because we
1824+
# do not want to overwrite the matplotlib docstring.
18441825
super().savefig(os.path.expanduser(filename), **kwargs)
18451826

18461827
def set_canvas(self, canvas):
@@ -1859,6 +1840,42 @@ def set_canvas(self, canvas):
18591840
canvas.print_figure = _canvas_preprocess(canvas, 'print_figure')
18601841
super().set_canvas(canvas)
18611842

1843+
def set_size_inches(self, w, h=None, forward=True, auto=False):
1844+
# Set the figure size and, if this is being called manually or from
1845+
# an interactive backend, override the geometry tracker so users can
1846+
# use interactive backends. See #76. Undocumented because this is
1847+
# only relevant internally.
1848+
# NOTE: Bitmap renderers use int(Figure.bbox.[width|height]) which
1849+
# rounds to whole pixels. So when renderer resizes the figure
1850+
# internally there may be roundoff error! Always compare to *both*
1851+
# Figure.get_size_inches() and the truncated bbox dimensions times dpi.
1852+
# Comparison is critical because most renderers call set_size_inches()
1853+
# before any resizing interaction!
1854+
if h is None:
1855+
width, height = w
1856+
else:
1857+
width, height = w, h
1858+
if not all(np.isfinite(_) for _ in (width, height)):
1859+
raise ValueError(
1860+
'Figure size must be finite, not ({width}, {height}).'
1861+
)
1862+
width_true, height_true = self.get_size_inches()
1863+
width_trunc = int(self.bbox.width) / self.dpi
1864+
height_trunc = int(self.bbox.height) / self.dpi
1865+
if auto:
1866+
with self._context_resizing():
1867+
super().set_size_inches(width, height, forward=forward)
1868+
else:
1869+
if ( # can have internal resizing not associated with any draws
1870+
(width not in (width_true, width_trunc)
1871+
or height not in (height_true, height_trunc))
1872+
and not self._is_resizing
1873+
and not self.canvas._is_idle_drawing # standard
1874+
and not getattr(self.canvas, '_draw_pending', None) # pyqt5
1875+
):
1876+
self._subplots_kw.update(width=width, height=height)
1877+
super().set_size_inches(width, height, forward=forward)
1878+
18621879
def set_alignx(self, value):
18631880
"""Set the *x* axis label alignment mode."""
18641881
self.stale = True
@@ -1928,42 +1945,6 @@ def ref(self, ref):
19281945
self.stale = True
19291946
self._ref = ref
19301947

1931-
def set_size_inches(self, w, h=None, forward=True, auto=False):
1932-
# Set the figure size and, if this is being called manually or from
1933-
# an interactive backend, override the geometry tracker so users can
1934-
# use interactive backends. See #76. Undocumented because this is
1935-
# only relevant internally.
1936-
# NOTE: Bitmap renderers use int(Figure.bbox.[width|height]) which
1937-
# rounds to whole pixels. So when renderer resizes the figure
1938-
# internally there may be roundoff error! Always compare to *both*
1939-
# Figure.get_size_inches() and the truncated bbox dimensions times dpi.
1940-
# Comparison is critical because most renderers call set_size_inches()
1941-
# before any resizing interaction!
1942-
if h is None:
1943-
width, height = w
1944-
else:
1945-
width, height = w, h
1946-
if not all(np.isfinite(_) for _ in (width, height)):
1947-
raise ValueError(
1948-
'Figure size must be finite, not ({width}, {height}).'
1949-
)
1950-
width_true, height_true = self.get_size_inches()
1951-
width_trunc = int(self.bbox.width) / self.dpi
1952-
height_trunc = int(self.bbox.height) / self.dpi
1953-
if auto:
1954-
with self._context_resizing():
1955-
super().set_size_inches(width, height, forward=forward)
1956-
else:
1957-
if ( # can have internal resizing not associated with any draws
1958-
(width not in (width_true, width_trunc)
1959-
or height not in (height_true, height_trunc))
1960-
and not self._is_resizing
1961-
and not self.canvas._is_idle_drawing # standard
1962-
and not getattr(self.canvas, '_draw_pending', None) # pyqt5
1963-
):
1964-
self._subplots_kw.update(width=width, height=height)
1965-
super().set_size_inches(width, height, forward=forward)
1966-
19671948
def _iter_axes(self):
19681949
"""Iterates over all axes and panels in the figure belonging to the
19691950
`~proplot.axes.Axes` class. Excludes inset and twin axes."""
@@ -1983,7 +1964,7 @@ def _iter_axes(self):
19831964

19841965

19851966
def _journals(journal):
1986-
"""Journal sizes for figures."""
1967+
"""Return the width and height corresponding to the given journal."""
19871968
# Get dimensions for figure from common journals.
19881969
value = JOURNAL_SPECS.get(journal, None)
19891970
if value is None:
@@ -2002,7 +1983,7 @@ def _journals(journal):
20021983

20031984

20041985
def _axes_dict(naxs, value, kw=False, default=None):
2005-
"""Build a dictionary that looks like ``{1:value1, 2:value2, ...}`` or
1986+
"""Return a dictionary that looks like ``{1:value1, 2:value2, ...}`` or
20061987
``{1:{key1:value1, ...}, 2:{key2:value2, ...}, ...}`` for storing
20071988
standardized axes-specific properties or keyword args."""
20081989
# First build up dictionary
@@ -2063,9 +2044,9 @@ def subplots(
20632044
**kwargs
20642045
):
20652046
"""
2066-
Create a figure with a single axes or arbitrary grid of axes, analogous
2067-
to `matplotlib.pyplot.subplots`. The axes can have arbitrary map
2068-
projections.
2047+
Create a figure with a single subplot or arbitrary grids of subplots,
2048+
analogous to `matplotlib.pyplot.subplots`. The subplots can be drawn with
2049+
arbitrary projections.
20692050
20702051
Parameters
20712052
----------

0 commit comments

Comments
 (0)