8000 Merge pull request #20107 from anntzer/mosaicshares · matplotlib/matplotlib@9177280 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9177280

Browse files
authored
Merge pull request #20107 from anntzer/mosaicshares
Support full-sharex/y in subplot_mosaic.
2 parents 8864491 + 1fd0914 commit 9177280

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
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': 10000 each subplot x- or y-axis will be independent.
@@ -1649,8 +1648,8 @@ def _normalize_grid_string(layout):
16491648
layout = inspect.cleandoc(layout)
16501649
return [list(ln) for ln in layout.strip('\n').split('\n')]
16511650

1652-
def subplot_mosaic(self, layout, *, subplot_kw=None, gridspec_kw=None,
1653-
empty_sentinel='.'):
1651+
def subplot_mosaic(self, layout, *, sharex=False, sharey=False,
1652+
subplot_kw=None, gridspec_kw=None, empty_sentinel='.'):
16541653
"""
16551654
Build a layout of Axes based on ASCII art or nested lists.
16561655
@@ -1661,7 +1660,6 @@ def subplot_mosaic(self, layout, *, subplot_kw=None, gridspec_kw=None,
16611660
This API is provisional and may be revised in the future based on
16621661
early user feedback.
16631662
1664-
16651663
Parameters
16661664
----------
16671665
layout : list of list of {hashable or nested} or str
@@ -1672,7 +1670,7 @@ def subplot_mosaic(self, layout, *, subplot_kw=None, gridspec_kw=None,
16721670
x = [['A panel', 'A panel', 'edge'],
16731671
['C panel', '.', 'edge']]
16741672
1675-
Produces 4 Axes:
1673+
produces 4 Axes:
16761674
16771675
- 'A panel' which is 1 row high and spans the first two columns
16781676
- 'edge' which is 2 rows high and is on the right edge
@@ -1698,6 +1696,12 @@ def subplot_mosaic(self, layout, *, subplot_kw=None, gridspec_kw=None,
16981696
The string notation allows only single character Axes labels and
16991697
does not support nesting but is very terse.
17001698
1699+
sharex, sharey : bool, default: False
1700+
If True, the x-axis (*sharex*) or y-axis (*sharey*) will be shared
1701+
among all subplots. In that case, tick label visibility and axis
1702+
units behave as for `subplots`. If False, each subplot's x- or
1703+
y-axis will be independent.
1704+
17011705
subplot_kw : dict, optional
17021706
Dictionary with keywords passed to the `.Figure.add_subplot` call
17031707
used to create each subplot.
@@ -1725,6 +1729,8 @@ def subplot_mosaic(self, layout, *, subplot_kw=None, gridspec_kw=None,
17251729
# special-case string input
17261730
if isinstance(layout, str):
17271731
layout = self._normalize_grid_string(layout)
1732+
# Only accept strict bools to allow a possible future API expansion.
1733+
_api.check_isinstance(bool, sharex=sharex, sharey=sharey)
17281734

17291735
def _make_array(inp):
17301736
"""
@@ -1882,6 +1888,14 @@ def _do_layout(gs, layout, unique_ids, nested):
18821888
rows, cols = layout.shape
18831889
gs = self.add_gridspec(rows, cols, **gridspec_kw)
18841890
ret = _do_layout(gs, layout, *_identify_keys_and_nested(layout))
1891+
ax0 = next(iter(ret.values()))
1892+
for ax in ret.values():
1893+
if sharex:
1894+
ax.sharex(ax0)
1895+
ax._label_outer_xaxis()
1896+
if sharey:
1897+
ax.sharey(ax0)
1898+
ax._label_outer_yaxis()
18851899
for k, ax in ret.items():
18861900
if isinstance(k, str):
18871901
ax.set_label(k)

lib/matplotlib/pyplot.py

Lines changed: 12 additions & 7 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
@@ -1444,7 +1445,6 @@ def subplot_mosaic(layout, *, subplot_kw=None, gridspec_kw=None,
14441445
This API is provisional and may be revised in the future based on
14451446
early user feedback.
14461447
1447-
14481448
Parameters
14491449
----------
14501450
layout : list of list of {hashable or nested} or str
@@ -1455,7 +1455,7 @@ def subplot_mosaic(layout, *, subplot_kw=None, gridspec_kw=None,
14551455
x = [['A panel', 'A panel', 'edge'],
14561456
['C panel', '.', 'edge']]
14571457
1458-
Produces 4 axes:
1458+
produces 4 axes:
14591459
14601460
- 'A panel' which is 1 row high and spans the first two columns
14611461
- 'edge' which is 2 rows high and is on the right edge
@@ -1476,6 +1476,12 @@ def subplot_mosaic(layout, *, subplot_kw=None, gridspec_kw=None,
14761476
This only allows only single character Axes labels and does
14771477
not allow nesting but is very terse.
14781478
1479+
sharex, sharey : bool, default: False
1480< B41A /td>+
If True, the x-axis (*sharex*) or y-axis (*sharey*) will be shared
1481+
among all subplots. In that case, tick label visibility and axis units
1482+
behave as for `subplots`. If False, each subplot's x- or y-axis will
1483+
be independent.
1484+
14791485
subplot_kw : dict, optional
14801486
Dictionary with keywords passed to the `.Figure.add_subplot` call
14811487
used to create each subplot.
@@ -1507,9 +1513,8 @@ def subplot_mosaic(layout, *, subplot_kw=None, gridspec_kw=None,
15071513
"""
15081514
fig = figure(**fig_kw)
15091515
ax_dict = fig.subplot_mosaic(
1510-
layout,
1511-
subplot_kw=subplot_kw,
1512-
gridspec_kw=gridspec_kw,
1516+
layout, sharex=sharex, sharey=sharey,
1517+
subplot_kw=subplot_kw, gridspec_kw=gridspec_kw,
15131518
empty_sentinel=empty_sentinel
15141519
)
15151520
return fig, ax_dict

lib/matplotlib/tests/test_figure.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,20 @@ def test_nested_user_order(self):
883883
assert list(ax_dict) == list("ABCDEFGHI")
884884
assert list(fig.axes) == list(ax_dict.values())
885885

886+
def test_share_all(self):
887+
layout = [
888+
["A", [["B", "C"],
889+
["D", "E"]]],
890+
["F", "G"],
891+
[".", [["H", [["I"],
892+
["."]]]]]
893+
]
894+
fig = plt.figure()
895+
ax_dict = fig.subplot_mosaic(layout, sharex=True, sharey=True)
896+
ax_dict["A"].set(xscale="log", yscale="logit")
897+
assert all(ax.get_xscale() == "log" and ax.get_yscale() == "logit"
898+
for ax in ax_dict.values())
899+
886900

887901
def test_reused_gridspec():
888902
"""Test that these all use the same gridspec"""

0 commit comments

Comments
 (0)
0