Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1fd0914

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

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': 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.
@@ -1748,6 +1752,8 @@ def subplot_mosaic(self, layout, *, subplot_kw=None, gridspec_kw=None,
17481752
# special-case string input
17491753
if isinstance(layout, str):
17501754
layout = self._normalize_grid_string(layout)
1755+
# Only accept strict bools to allow a possible future API expansion.
1756+
_api.check_isinstance(bool, sharex=sharex, sharey=sharey)
17511757

17521758
def _make_array(inp):
17531759
"""
@@ -1905,6 +1911,14 @@ def _do_layout(gs, layout, unique_ids, nested):
19051911
rows, cols = layout.shape
19061912
gs = self.add_gridspec(rows, cols, **gridspec_kw)
19071913
ret = _do_layout(gs, layout, *_identify_keys_and_nested(layout))
1914+
ax0 = next(iter(ret.values()))
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: 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+
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)