Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fd8c87e

Browse files
authored
Merge pull request #18453 from jklymak/fix-manual-place
FIX: allow manually placed axes in constrained_layout
2 parents 6a75087 + 81d9ce4 commit fd8c87e

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

lib/matplotlib/_constrained_layout.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def _make_layout_margins(fig, renderer, *, w_pad=0, h_pad=0,
209209

210210
# for ax in [a for a in fig._localaxes if hasattr(a, 'get_subplotspec')]:
211211
for ax in fig.get_axes():
212-
if not hasattr(ax, 'get_subplotspec'):
212+
if not hasattr(ax, 'get_subplotspec') or not ax.get_in_layout():
213213
continue
214214

215215
ss = ax.get_subplotspec()
@@ -320,7 +320,8 @@ def _match_submerged_margins(fig):
320320
for panel in fig.panels:
321321
_match_submerged_margins(panel)
322322

323-
axs = [a for a in fig.get_axes() if hasattr(a, 'get_subplotspec')]
323+
axs = [a for a in fig.get_axes() if (hasattr(a, 'get_subplotspec')
324+
and a.get_in_layout())]
324325

325326
for ax1 in axs:
326327
ss1 = ax1.get_subplotspec()
@@ -457,7 +458,7 @@ def _reposition_axes(fig, renderer, *, w_pad=0, h_pad=0, hspace=0, wspace=0):
457458
# for ax in fig._localaxes:
458459
# if not hasattr(a, 'get_subplotspec'):
459460
for ax in fig.get_axes():
460-
if not hasattr(ax, 'get_subplotspec'):
461+
if not hasattr(ax, 'get_subplotspec') or not ax.get_in_layout():
461462
continue
462463

463464
# grid bbox is in Figure co-ordinates, but we specify in panel
@@ -578,7 +579,7 @@ def _reset_margins(fig):
578579
for span in fig.panels:
579580
_reset_margins(span)
580581
for ax in fig.axes:
581-
if hasattr(ax, 'get_subplotspec'):
582+
if hasattr(ax, 'get_subplotspec') and ax.get_in_layout():
582583
ss = ax.get_subplotspec()
583584
gs = ss.get_gridspec()
584585
if gs._layoutgrid is not None:

lib/matplotlib/axes/_base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,8 @@ def set_position(self, pos, which='both'):
926926
"""
927927
self._set_position(pos, which=which)
928928
# because this is being called externally to the library we
929-
# zero the constrained layout parts.
929+
# don't let it be in the layout.
930+
self.set_in_layout(False)
930931

931932
def _set_position(self, pos, which='both'):
932933
"""

lib/matplotlib/tests/test_constrainedlayout.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,3 +487,19 @@ def test_colorbars_no_overlapH():
487487
ax.tick_params(axis='both', direction='in')
488488
im = ax.imshow([[1, 2], [3, 4]])
489489
fig.colorbar(im, ax=ax, orientation="horizontal")
490+
491+
492+
def test_manually_set_position():
493+
fig, axs = plt.subplots(1, 2, constrained_layout=True)
494+
axs[0].set_position([0.2, 0.2, 0.3, 0.3])
495+
fig.canvas.draw()
496+
pp = axs[0].get_position()
497+
np.testing.assert_allclose(pp, [[0.2, 0.2], [0.5, 0.5]])
498+
499+
fig, axs = plt.subplots(1, 2, constrained_layout=True)
500+
axs[0].set_position([0.2, 0.2, 0.3, 0.3])
501+
pc = axs[0].pcolormesh(np.random.rand(20, 20))
502+
fig.colorbar(pc, ax=axs[0])
503+
fig.canvas.draw()
504+
pp = axs[0].get_position()
505+
np.testing.assert_allclose(pp, [[0.2, 0.2], [0.44, 0.5]])

0 commit comments

Comments
 (0)