Skip to content

Commit 4a2ea81

Browse files
authored
Fix/mpl37 compat (#52150)
* FIX: skip Axes not in subplot layouts As of mpl 3.7 all Axes now report as being instances of Subplots, however ax.get_subplotspec() may now return None if the Axes is not actually in gridspec. * MNT: avoid unneeded indirection The MPLPlot instance already holds a reference to the Figure, use that instead of walking up from the first Axes. * TST: add test of trying to layout a colorbar
1 parent db313f2 commit 4a2ea81

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

pandas/plotting/_matplotlib/core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,9 @@ def _get_subplots(self):
11121112
from matplotlib.axes import Subplot
11131113

11141114
return [
1115-
ax for ax in self.axes[0].get_figure().get_axes() if isinstance(ax, Subplot)
1115+
ax
1116+
for ax in self.fig.get_axes()
1117+
if (isinstance(ax, Subplot) and ax.get_subplotspec() is not None)
11161118
]
11171119

11181120
def _get_axes_layout(self) -> tuple[int, int]:

pandas/tests/plotting/test_common.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,22 @@ def test__gen_two_subplots_with_ax(self):
4040
subplot_geometry = list(axes[0].get_subplotspec().get_geometry()[:-1])
4141
subplot_geometry[-1] += 1
4242
assert subplot_geometry == [2, 1, 2]
43+
44+
def test_colorbar_layout(self):
45+
fig = self.plt.figure()
46+
47+
axes = fig.subplot_mosaic(
48+
"""
49+
AB
50+
CC
51+
"""
52+
)
53+
54+
x = [1, 2, 3]
55+
y = [1, 2, 3]
56+
57+
cs0 = axes["A"].scatter(x, y)
58+
axes["B"].scatter(x, y)
59+
60+
fig.colorbar(cs0, ax=[axes["A"], axes["B"]], location="right")
61+
DataFrame(x).plot(ax=axes["C"])

0 commit comments

Comments
 (0)