8000 Fix label_outer in the presence of colorbars. · matplotlib/matplotlib@19ea1dd · GitHub
[go: up one dir, main page]

Skip to content

Commit 19ea1dd

Browse files
committed
Fix label_outer in the presence of colorbars.
The subgridspec to be considered should be the one containing both the axes and the colorbar, not the sub-subgridspec of just the axes.
1 parent 903d537 commit 19ea1dd

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4749,13 +4749,37 @@ 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+
if not ss:
4760+
return
4761+
gs = ss.get_gridspec()
4762+
if (isinstance(gs, mpl.gridspec.GridSpecFromSubplotSpec)
4763+
and gs.nrows * gs.ncols == 6):
4764+
for ax in self.figure.axes:
4765+
if (ax is not self
4766+
and hasattr(ax, "_colorbar_info")
4767+
and ax.get_subplotspec()
4768+
and isinstance(ax.get_subplotspec().get_gridspec(),
4769+
mpl.gridspec.GridSpecFromSubplotSpec)
4770+
and (ax.get_subplotspec().get_gridspec()._subplot_spec
4771+
is gs._subplot_spec)):
4772+
ss = gs._subplot_spec
4773+
break
4774+
return ss
4775+
47524776
def _label_outer_xaxis(self, *, skip_non_rectangular_axes,
47534777
remove_inner_ticks=False):
47544778
# see documentation in label_outer.
47554779
if skip_non_rectangular_axes and not isinstance(self.patch,
47564780
mpl.patches.Rectangle):
47574781
return
4758-
ss = self.get_subplotspec()
4782+
ss = self._get_subplotspec_with_optional_colorbar()
47594783
if not ss:
47604784
return
47614785
label_position = self.xaxis.get_label_position()
@@ -4782,7 +4806,7 @@ def _label_outer_yaxis(self, *, skip_non_rectangular_axes,
47824806
if skip_non_rectangular_axes and not isinstance(self.patch,
47834807
mpl.patches.Rectangle):
47844808
return
4785-
ss = self.get_subplotspec()
4809+
ss = self._get_subplotspec_with_optional_colorbar()
47864810
if not ss:
47874811
return
47884812
label_position = self.yaxis.get_label_position()

lib/matplotlib/tests/test_subplots.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import numpy as np
55
import pytest
66

7+
import matplotlib as mpl
78
from matplotlib.axes import Axes, SubplotBase
89
import matplotlib.pyplot as plt
910
from matplotlib.testing.decorators import check_figures_equal, image_comparison
@@ -111,10 +112,13 @@ def test_shared():
111112

112113

113114
@pytest.mark.parametrize('remove_ticks', [True, False])
114-
def test_label_outer(remove_ticks):
115-
f, axs = plt.subplots(2, 2, sharex=True, sharey=True)
115+
@pytest.mark.parametrize('with_colorbar', [True, False])
116+
def test_label_outer(remove_ticks, with_colorbar):
117+
fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)
116118
for ax in axs.flat:
117119
ax.set(xlabel="foo", ylabel="bar")
120+
if with_colorbar:
121+
fig.colorbar(mpl.cm.ScalarMappable(), ax=ax)
118122
ax.label_outer(remove_inner_ticks=remove_ticks)
119123
check_ticklabel_visible(
120124
axs.flat, [False, False, True, True], [True, False, True, False])

0 commit comments

Comments
 (0)
0