From 000a42f595aa932bb5216e598065492b88f92948 Mon Sep 17 00:00:00 2001 From: "Lumberbot (aka Jack)" <39504233+meeseeksmachine@users.noreply.github.com> Date: Sun, 25 Jun 2023 21:04:49 -0700 Subject: [PATCH] Backport PR #52150 on branch 2.0.x (Fix/mpl37 compat) (#53850) Backport PR #52150: Fix/mpl37 compat Co-authored-by: Thomas A Caswell --- pandas/plotting/_matplotlib/core.py | 4 +++- pandas/tests/plotting/test_common.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 754cc94b6ded6..88244018079d2 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1112,7 +1112,9 @@ def _get_subplots(self): from matplotlib.axes import Subplot return [ - ax for ax in self.axes[0].get_figure().get_axes() if isinstance(ax, Subplot) + ax + for ax in self.fig.get_axes() + if (isinstance(ax, Subplot) and ax.get_subplotspec() is not None) ] def _get_axes_layout(self) -> tuple[int, int]: diff --git a/pandas/tests/plotting/test_common.py b/pandas/tests/plotting/test_common.py index d4624cfc74872..faf8278675566 100644 --- a/pandas/tests/plotting/test_common.py +++ b/pandas/tests/plotting/test_common.py @@ -40,3 +40,22 @@ def test__gen_two_subplots_with_ax(self): subplot_geometry = list(axes[0].get_subplotspec().get_geometry()[:-1]) subplot_geometry[-1] += 1 assert subplot_geometry == [2, 1, 2] + + def test_colorbar_layout(self): + fig = self.plt.figure() + + axes = fig.subplot_mosaic( + """ + AB + CC + """ + ) + + x = [1, 2, 3] + y = [1, 2, 3] + + cs0 = axes["A"].scatter(x, y) + axes["B"].scatter(x, y) + + fig.colorbar(cs0, ax=[axes["A"], axes["B"]], location="right") + DataFrame(x).plot(ax=axes["C"])