Skip to content

Commit 36b9514

Browse files
timhoffmmeeseeksmachine
authored andcommitted
Backport PR #23203: Honour panchor keyword for colorbar on subplot
1 parent 23dfa16 commit 36b9514

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

lib/matplotlib/colorbar.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,8 @@ def make_axes_gridspec(parent, *, location=None, orientation=None,
15991599
aspect = 1 / aspect
16001600

16011601
parent.set_subplotspec(ss_main)
1602-
parent.set_anchor(loc_settings["panchor"])
1602+
if panchor is not False:
1603+
parent.set_anchor(panchor)
16031604

16041605
fig = parent.get_figure()
16051606
cax = fig.add_subplot(ss_cb, label="<colorbar>")

lib/matplotlib/tests/test_colorbar.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,23 @@ def test_colorbar_positioning(use_gridspec):
211211

212212

213213
def test_colorbar_single_ax_panchor_false():
214-
# Just smoketesting that this doesn't crash. Note that this differs from
215-
# the tests above with panchor=False because there use_gridspec is actually
216-
# ineffective: passing *ax* as lists always disable use_gridspec.
214+
# Note that this differs from the tests above with panchor=False because
215+
# there use_gridspec is actually ineffective: passing *ax* as lists always
216+
# disables use_gridspec.
217+
ax = plt.subplot(111, anchor='N')
217218
plt.imshow([[0, 1]])
218219
plt.colorbar(panchor=False)
220+
assert ax.get_anchor() == 'N'
221+
222+
223+
@pytest.mark.parametrize('constrained', [False, True],
224+
ids=['standard', 'constrained'])
225+
def test_colorbar_single_ax_panchor_east(constrained):
226+
fig = plt.figure(constrained_layout=constrained)
227+
ax = fig.add_subplot(111, anchor='N')
228+
plt.imshow([[0, 1]])
229+
plt.colorbar(panchor='E')
230+
assert ax.get_anchor() == 'E'
219231

220232

221233
@image_comparison(['contour_colorbar.png'], remove_text=True)

0 commit comments

Comments
 (0)