From 392d4eaccbddc8893a7e3743681085c74729c6e2 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Sat, 4 Jun 2022 20:14:43 +0100 Subject: [PATCH] honour panchor keyword for colorbar on subplot --- lib/matplotlib/colorbar.py | 3 ++- lib/matplotlib/tests/test_colorbar.py | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index c59b0ac815fe..7dee781f5f97 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1617,7 +1617,8 @@ def make_axes_gridspec(parent, *, location=None, orientation=None, aspect = 1 / aspect parent.set_subplotspec(ss_main) - parent.set_anchor(loc_settings["panchor"]) + if panchor is not False: + parent.set_anchor(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 ae3ab92c0d43..29ea00530418 100644 --- a/lib/matplotlib/tests/test_colorbar.py +++ b/lib/matplotlib/tests/test_colorbar.py @@ -211,11 +211,23 @@ def test_colorbar_positioning(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. + # Note that this differs from the tests above with panchor=False because + # there use_gridspec is actually ineffective: passing *ax* as lists always + # disables use_gridspec. + ax = plt.subplot(111, anchor='N') plt.imshow([[0, 1]]) plt.colorbar(panchor=False) + assert ax.get_anchor() == 'N' + + +@pytest.mark.parametrize('constrained', [False, True], + ids=['standard', 'constrained']) +def test_colorbar_single_ax_panchor_east(constrained): + fig = plt.figure(constrained_layout=constrained) + ax = fig.add_subplot(111, anchor='N') + plt.imshow([[0, 1]]) + plt.colorbar(panchor='E') + assert ax.get_anchor() == 'E' @image_comparison(['contour_colorbar.png'], remove_text=True)