Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitadd6bbf

Browse files
committed
Fix label_outer in the presence of colorbars.
The subgridspec to be considered should be the one containing both theaxes and the colorbar, not the sub-subgridspec of just the axes.
1 parent903d537 commitadd6bbf

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

‎lib/matplotlib/axes/_base.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4749,14 +4749,39 @@ def label_outer(self, remove_inner_ticks=False):
47494749
self._label_outer_yaxis(skip_non_rectangular_axes=False,
47504750
remove_inner_ticks=remove_inner_ticks)
47514751

4752+
def_get_subplotspec_with_optional_colorbar(self):
4753+
"""
4754+
Return the subplotspec for this Axes, except that if this Axes has been
4755+
moved to a subgridspec to make room for a colorbar, then return the
4756+
subplotspec that encloses both this Axes and the colorbar Axes.
4757+
"""
4758+
ss=self.get_subplotspec()
4759+
ifnotss:
4760+
return
4761+
gs=ss.get_gridspec()
4762+
# Match against subgridspec hierarchy set up by colorbar.make_axes_gridspec.
4763+
if (isinstance(gs,mpl.gridspec.GridSpecFromSubplotSpec)
4764+
andgs.nrows*gs.ncols==6):
4765+
foraxinself.figure.axes:
4766+
if (axisnotself
4767+
andhasattr(ax,"_colorbar_info")
4768+
andax.get_subplotspec()
4769+
andisinstance(ax.get_subplotspec().get_gridspec(),
4770+
mpl.gridspec.GridSpecFromSubplotSpec)
4771+
and (ax.get_subplotspec().get_gridspec()._subplot_spec
4772+
isgs._subplot_spec)):
4773+
ss=gs._subplot_spec
4774+
break
4775+
returnss
4776+
47524777
def_label_outer_xaxis(self,*,skip_non_rectangular_axes,
47534778
remove_inner_ticks=False):
47544779
# see documentation in label_outer.
47554780
ifskip_non_rectangular_axesandnotisinstance(self.patch,
47564781
mpl.patches.Rectangle):
47574782
return
4758-
ss=self.get_subplotspec()
4759-
ifnotss:
4783+
ss=self._get_subplotspec_with_optional_colorbar()
4784+
ifssisNone:
47604785
return
47614786
label_position=self.xaxis.get_label_position()
47624787
ifnotss.is_first_row():# Remove top label/ticklabels/offsettext.
@@ -4782,8 +4807,8 @@ def _label_outer_yaxis(self, *, skip_non_rectangular_axes,
47824807
ifskip_non_rectangular_axesandnotisinstance(self.patch,
47834808
mpl.patches.Rectangle):
47844809
return
4785-
ss=self.get_subplotspec()
4786-
ifnotss:
4810+
ss=self._get_subplotspec_with_optional_colorbar()
4811+
ifssisNone:
47874812
return
47884813
label_position=self.yaxis.get_label_position()
47894814
ifnotss.is_first_col():# Remove left label/ticklabels/offsettext.

‎lib/matplotlib/tests/test_subplots.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
importnumpyasnp
55
importpytest
66

7+
importmatplotlibasmpl
78
frommatplotlib.axesimportAxes,SubplotBase
89
importmatplotlib.pyplotasplt
910
frommatplotlib.testing.decoratorsimportcheck_figures_equal,image_comparison
@@ -111,10 +112,15 @@ def test_shared():
111112

112113

113114
@pytest.mark.parametrize('remove_ticks', [True,False])
114-
deftest_label_outer(remove_ticks):
115-
f,axs=plt.subplots(2,2,sharex=True,sharey=True)
115+
@pytest.mark.parametrize('layout_engine', ['tight','constrained'])
116+
@pytest.mark.parametrize('with_colorbar', [True,False])
117+
deftest_label_outer(remove_ticks,layout_engine,with_colorbar):
118+
fig=plt.figure(layout=layout_engine)
119+
axs=fig.subplots(2,2,sharex=True,sharey=True)
116120
foraxinaxs.flat:
117121
ax.set(xlabel="foo",ylabel="bar")
122+
ifwith_colorbar:
123+
fig.colorbar(mpl.cm.ScalarMappable(),ax=ax)
118124
ax.label_outer(remove_inner_ticks=remove_ticks)
119125
check_ticklabel_visible(
120126
axs.flat, [False,False,True,True], [True,False,True,False])

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp