Skip to content

Commit 1e5b9fd

Browse files
committed
Support full-sharex/y in subplot_mosaic.
1 parent 0ac5c29 commit 1e5b9fd

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

lib/matplotlib/figure.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -807,8 +807,7 @@ def subplots(self, nrows=1, ncols=1, sharex=False, sharey=False,
807807
Number of rows/columns of the subplot grid.
808808
809809
sharex, sharey : bool or {'none', 'all', 'row', 'col'}, default: False
810-
Controls sharing of properties among x (*sharex*) or y (*sharey*)
811-
axes:
810+
Controls sharing of x-axis (*sharex*) or y-axis (*sharey*):
812811
813812
- True or 'all': x- or y-axis will be shared among all subplots.
814813
- False or 'none': each subplot x- or y-axis will be independent.
@@ -1672,8 +1671,8 @@ def _normalize_grid_string(layout):
16721671
layout = inspect.cleandoc(layout)
16731672
return [list(ln) for ln in layout.strip('\n').split('\n')]
16741673

1675-
def subplot_mosaic(self, layout, *, subplot_kw=None, gridspec_kw=None,
1676-
empty_sentinel='.'):
1674+
def subplot_mosaic(self, layout, *, sharex=False, sharey=False,
1675+
subplot_kw=None, gridspec_kw=None, empty_sentinel='.'):
16771676
"""
16781677
Build a layout of Axes based on ASCII art or nested lists.
16791678
@@ -1684,7 +1683,6 @@ def subplot_mosaic(self, layout, *, subplot_kw=None, gridspec_kw=None,
16841683
This API is provisional and may be revised in the future based on
16851684
early user feedback.
16861685
1687-
16881686
Parameters
16891687
----------
16901688
layout : list of list of {hashable or nested} or str
@@ -1695,7 +1693,7 @@ def subplot_mosaic(self, layout, *, subplot_kw=None, gridspec_kw=None,
16951693
x = [['A panel', 'A panel', 'edge'],
16961694
['C panel', '.', 'edge']]
16971695
1698-
Produces 4 Axes:
1696+
produces 4 Axes:
16991697
17001698
- 'A panel' which is 1 row high and spans the first two columns
17011699
- 'edge' which is 2 rows high and is on the right edge
@@ -1721,6 +1719,12 @@ def subplot_mosaic(self, layout, *, subplot_kw=None, gridspec_kw=None,
17211719
The string notation allows only single character Axes labels and
17221720
does not support nesting but is very terse.
17231721
1722+
sharex, sharey : bool, default: False
1723+
If True, the x-axis (*sharex*) or y-axis (*sharey*) will be shared
1724+
among all subplots. In that case, tick label visibility and axis
1725+
units behave as for `subplots`. If False, each subplot's x- or
1726+
y-axis will be independent.
1727+
17241728
subplot_kw : dict, optional
17251729
Dictionary with keywords passed to the `.Figure.add_subplot` call
17261730
used to create each subplot.
@@ -1905,6 +1909,16 @@ def _do_layout(gs, layout, unique_ids, nested):
19051909
rows, cols = layout.shape
19061910
gs = self.add_gridspec(rows, cols, **gridspec_kw)
19071911
ret = _do_layout(gs, layout, *_identify_keys_and_nested(layout))
1912+
ax0 = next(iter(ret.values()))
1913+
# Only accept strict bools to allow a possible future API expansion.
1914+
_api.check_isinstance(bool, sharex=sharex, sharey=sharey)
1915+
for ax in ret.values():
1916+
if sharex:
1917+
ax.sharex(ax0)
1918+
ax._label_outer_xaxis()
1919+
if sharey:
1920+
ax.sharey(ax0)
1921+
ax._label_outer_yaxis()
19081922
for k, ax in ret.items():
19091923
if isinstance(k, str):
19101924
ax.set_label(k)

lib/matplotlib/pyplot.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,8 +1432,9 @@ def subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True,
14321432
return fig, axs
14331433

14341434

1435-
def subplot_mosaic(layout, *, subplot_kw=None, gridspec_kw=None,
1436-
empty_sentinel='.', **fig_kw):
1435+
def subplot_mosaic(layout, *, sharex=False, sharey=False,
1436+
subplot_kw=None, gridspec_kw=None, empty_sentinel='.',
1437+
**fig_kw):
14371438
"""
14381439
Build a layout of Axes based on ASCII art or nested lists.
14391440
@@ -1476,6 +1477,12 @@ def subplot_mosaic(layout, *, subplot_kw=None, gridspec_kw=None,
14761477
This only allows only single character Axes labels and does
14771478
not allow nesting but is very terse.
14781479
1480+
sharex, sharey : bool, default: False
1481+
If True, the x-axis (*sharex*) or y-axis (*sharey*) will be shared
1482+
among all subplots. In that case, tick label visibility and axis units
1483+
behave as for `subplots`. If False, each subplot's x- or y-axis will
1484+
be independent.
1485+
14791486
subplot_kw : dict, optional
14801487
Dictionary with keywords passed to the `.Figure.add_subplot` call
14811488
used to create each subplot.
@@ -1507,9 +1514,8 @@ def subplot_mosaic(layout, *, subplot_kw=None, gridspec_kw=None,
15071514
"""
15081515
fig = figure(**fig_kw)
15091516
ax_dict = fig.subplot_mosaic(
1510-
layout,
1511-
subplot_kw=subplot_kw,
1512-
gridspec_kw=gridspec_kw,
1517+
layout, sharex=sharex, sharey=sharey,
1518+
subplot_kw=subplot_kw, gridspec_kw=gridspec_kw,
15131519
empty_sentinel=empty_sentinel
15141520
)
15151521
return fig, ax_dict

0 commit comments

Comments
 (0)