Skip to content

Commit 3ea5b63

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 fed8c20 commit 3ea5b63

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

lib/matplotlib/axes/_base.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4749,14 +4749,25 @@ 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 any(cax.get_subplotspec() for cax in self._colorbars):
4760+
ss = ss.get_gridspec()._subplot_spec
4761+
return ss
4762+
47524763
def _label_outer_xaxis(self, *, skip_non_rectangular_axes,
47534764
remove_inner_ticks=False):
47544765
# see documentation in label_outer.
47554766
if skip_non_rectangular_axes and not isinstance(self.patch,
47564767
mpl.patches.Rectangle):
47574768
return
4758-
ss = self.get_subplotspec()
4759-
if not ss:
4769+
ss = self._get_subplotspec_with_optional_colorbar()
4770+
if ss is None:
47604771
return
47614772
label_position = self.xaxis.get_label_position()
47624773
if not ss.is_first_row(): # Remove top label/ticklabels/offsettext.
@@ -4782,8 +4793,8 @@ def _label_outer_yaxis(self, *, skip_non_rectangular_axes,
47824793
if skip_non_rectangular_axes and not isinstance(self.patch,
47834794
mpl.patches.Rectangle):
47844795
return
4785-
ss = self.get_subplotspec()
4786-
if not ss:
4796+
ss = self._get_subplotspec_with_optional_colorbar()
4797+
if ss is None:
47874798
return
47884799
label_position = self.yaxis.get_label_position()
47894800
if not ss.is_first_col(): # Remove left label/ticklabels/offsettext.

lib/matplotlib/colorbar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,8 +1455,7 @@ def make_axes(parents, location=None, orientation=None, fraction=0.15,
14551455

14561456
cax = fig.add_axes(pbcb, label="<colorbar>")
14571457
for a in parents:
1458-
# tell the parent it has a colorbar
1459-
a._colorbars += [cax]
1458+
a._colorbars.append(cax) # tell the parent it has a colorbar
14601459
cax._colorbar_info = dict(
14611460
parents=parents,
14621461
location=location,
@@ -1549,6 +1548,7 @@ def make_axes_gridspec(parent, *, location=None, orientation=None,
15491548

15501549
fig = parent.get_figure()
15511550
cax = fig.add_subplot(ss_cb, label="<colorbar>")
1551+
parent._colorbars.append(cax) # tell the parent it has a colorbar
15521552
cax.set_anchor(anchor)
15531553
cax.set_box_aspect(aspect)
15541554
cax.set_aspect('auto')

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
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,15 @@ 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('layout_engine', ['none', 'tight', 'constrained'])
116+
@pytest.mark.parametrize('with_colorbar', [True, False])
117+
def test_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
for ax in axs.flat:
117121
ax.set(xlabel="foo", ylabel="bar")
122+
if with_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)