From 357979ebd535a93a981ad1b7e927e4e353952b85 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 2 Apr 2022 18:31:38 +0200 Subject: [PATCH] Fix colorbar stealing from a single axes and with panchor=False. This was accidentally broken in 16d639b: `panchor` and `loc_settings["panchor"]` can be different, so it is incorrect to replace one by the other. --- lib/matplotlib/colorbar.py | 2 +- lib/matplotlib/tests/test_colorbar.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index 89c86981d3ff..c59b0ac815fe 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1617,7 +1617,7 @@ def make_axes_gridspec(parent, *, location=None, orientation=None, aspect = 1 / aspect parent.set_subplotspec(ss_main) - parent.set_anchor(panchor) + parent.set_anchor(loc_settings["panchor"]) fig = parent.get_figure() cax = fig.add_subplot(ss_cb, label="") diff --git a/lib/matplotlib/tests/test_colorbar.py b/lib/matplotlib/tests/test_colorbar.py index 20d94c1ac3a7..d95e15903f51 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -210,6 +210,14 @@ def test_colorbar_positioning(use_gridspec): anchor=(0.8, 0.5), shrink=0.6, use_gridspec=use_gridspec) +def test_colorbar_single_ax_panchor_false(): + # Just smoketesting that this doesn't crash. Note that this differs from + # the tests above with panchor=False because there use_gridspec is actually + # ineffective: passing *ax* as lists always disable use_gridspec. + plt.imshow([[0, 1]]) + plt.colorbar(panchor=False) + + @image_comparison(['contour_colorbar.png'], remove_text=True) def test_contour_colorbar(): fig, ax = plt.subplots(figsize=(4, 2))