<h3 dir="auto">Bug summary</h3>
<p dir="auto">I use the <code class="notranslate">rect</code> parameter of the figure's layout engine to set figure margins. For figures with <code class="notranslate">layout='constrained'</code> this parameter appears to be ignored if <code class="notranslate">rcParams['figure.constrained_layout.use']</code> is <code class="notranslate">True</code> when saving the figure.</p>
<h3 dir="auto">Code for reproduction</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import matplotlib.pyplot as plt
plt.rcParams['figure.constrained_layout.use']=True
fig, ax = plt.subplots()
engine = fig.get_layout_engine()
engine.set(rect=[0.1,0.1,0.8,0.8]) # (left, bottom, width, height)
engine.execute(fig)
fig.savefig('test-constrained-True.png')"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>
<span class="pl-s1">plt</span>.<span class="pl-s1">rcParams</span>[<span class="pl-s">'figure.constrained_layout.use'</span>]<span class="pl-c1">=</span><span class="pl-c1">True</span>
<span class="pl-s1">fig</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">subplots</span>()
<span class="pl-s1">engine</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-en">get_layout_engine</span>()
<span class="pl-s1">engine</span>.<span class="pl-en">set</span>(<span class="pl-s1">rect</span><span class="pl-c1">=</span>[<span class="pl-c1">0.1</span>,<span class="pl-c1">0.1</span>,<span class="pl-c1">0.8</span>,<span class="pl-c1">0.8</span>]) <span class="pl-c"># (left, bottom, width, height)</span>
<span class="pl-s1">engine</span>.<span class="pl-en">execute</span>(<span class="pl-s1">fig</span>)
<span class="pl-s1">fig</span>.<span class="pl-en">savefig</span>(<span class="pl-s">'test-constrained-True.png'</span>)</pre></div>
<h3 dir="auto">Actual outcome</h3>
<p dir="auto">The saved figure does not have the expected layout. It does not match the figure displayed interactively. The axes extend to the edges of the figure area.<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/4016348/200223680-cb1cba6b-ee61-4533-94f4-5b0f46441ca9.png"><img src="https://user-images.githubusercontent.com/4016348/200223680-cb1cba6b-ee61-4533-94f4-5b0f46441ca9.png" alt="test-constrained-True" style="max-width: 100%;"></a></p>
<h3 dir="auto">Expected outcome</h3>
<p dir="auto">The saved figure has matches what is shown interactively. The axes do not extend to the edges of the figure area, respecting the <code class="notranslate">rect</code> parameter. This saved figure is generated with the code, which does not demonstrate the bug:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="plt.rcParams['figure.constrained_layout.use'] = False
fig, ax = plt.subplots(layout='constrained')
engine = fig.get_layout_engine()
engine.set(rect=[0.1,0.1,0.8,0.8]) # (left, bottom, width, height)
engine.execute(fig)
fig.savefig('test-constrained-False.png')"><pre class="notranslate"><span class="pl-s1">plt</span>.<span class="pl-s1">rcParams</span>[<span class="pl-s">'figure.constrained_layout.use'</span>] <span class="pl-c1">=</span> <span class="pl-c1">False</span>
<span class="pl-s1">fig</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">subplots</span>(<span class="pl-s1">layout</span><span class="pl-c1">=</span><span class="pl-s">'constrained'</span>)
<span class="pl-s1">engine</span> <span class="pl-c1">=</span> <span class="pl-s1">fig</span>.<span class="pl-en">get_layout_engine</span>()
<span class="pl-s1">engine</span>.<span class="pl-en">set</span>(<span class="pl-s1">rect</span><span class="pl-c1">=</span>[<span class="pl-c1">0.1</span>,<span class="pl-c1">0.1</span>,<span class="pl-c1">0.8</span>,<span class="pl-c1">0.8</span>]) <span class="pl-c"># (left, bottom, width, height)</span>
<span class="pl-s1">engine</span>.<span class="pl-en">execute</span>(<span class="pl-s1">fig</span>)
<span class="pl-s1">fig</span>.<span class="pl-en">savefig</span>(<span class="pl-s">'test-constrained-False.png'</span>)</pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/4016348/200223804-509d7fec-37a0-41fb-aede-541852706cbf.png"><img src="https://user-images.githubusercontent.com/4016348/200223804-509d7fec-37a0-41fb-aede-541852706cbf.png" alt="test-constrained-False" style="max-width: 100%;"></a></p>
<h3 dir="auto">Additional information</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Operating system</h3>
<p dir="auto">Windows</p>
<h3 dir="auto">Matplotlib Version</h3>
<p dir="auto">3.6.1</p>
<h3 dir="auto">Matplotlib Backend</h3>
<p dir="auto">Qt5Agg</p>
<h3 dir="auto">Python version</h3>
<p dir="auto">3.10.6</p>
<h3 dir="auto">Jupyter version</h3>
<p dir="auto">n/a</p>
<h3 dir="auto">Installation</h3>
<p dir="auto">conda</p>
<h3 dir="auto">Bug summary</h3>
<p dir="auto">When setting <code class="notranslate">rcParams['figure.constrained_layout.use'] = True</code> and <code class="notranslate">ax.set_aspect('equal')</code>, and saving with <code class="notranslate">bbox_inches = 'tight'</code>, the resulting figure is cropped incorrectly.</p>
<h3 dir="auto">Code for reproduction</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import matplotlib as mpl
import matplotlib.pyplot as plt

mpl.rcParams['figure.constrained_layout.use'] = True

fig, ax = plt.subplots()

ax.plot([0, 1])
ax.set_xlim(0, 1)
ax.set_aspect('equal')

fig.savefig('test.png', bbox_inches = 'tight')"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">matplotlib</span> <span class="pl-k">as</span> <span class="pl-s1">mpl</span>
<span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>

<span class="pl-s1">mpl</span>.<span class="pl-s1">rcParams</span>[<span class="pl-s">'figure.constrained_layout.use'</span>] <span class="pl-c1">=</span> <span class="pl-c1">True</span>

<span class="pl-s1">fig</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">subplots</span>()

<span class="pl-s1">ax</span>.<span class="pl-en">plot</span>([<span class="pl-c1">0</span>, <span class="pl-c1">1</span>])
<span class="pl-s1">ax</span>.<span class="pl-en">set_xlim</span>(<span class="pl-c1">0</span>, <span class="pl-c1">1</span>)
<span class="pl-s1">ax</span>.<span class="pl-en">set_aspect</span>(<span class="pl-s">'equal'</span>)

<span class="pl-s1">fig</span>.<span class="pl-en">savefig</span>(<span class="pl-s">'test.png'</span>, <span class="pl-s1">bbox_inches</span> <span class="pl-c1">=</span> <span class="pl-s">'tight'</span>)</pre></div>
<h3 dir="auto">Actual outcome</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="MatplotlibDeprecationWarning: The resize_event function was deprecated in Matplotlib 3.6 and will be removed two minor releases later. Use callbacks.process('resize_event', ResizeEvent(...)) instead.
  fig, ax = plt.subplots()"><pre class="notranslate"><code class="notranslate">MatplotlibDeprecationWarning: The resize_event function was deprecated in Matplotlib 3.6 and will be removed two minor releases later. Use callbacks.process('resize_event', ResizeEvent(...)) instead.
  fig, ax = plt.subplots()
</code></pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/64837956/194764228-8b349e42-0ddb-48a8-ba59-a1827cb3f4d3.png"><img src="https://user-images.githubusercontent.com/64837956/194764228-8b349e42-0ddb-48a8-ba59-a1827cb3f4d3.png" alt="test" style="max-width: 100%;"></a></p>
<h3 dir="auto">Expected outcome</h3>
<p dir="auto">If I leave out <code class="notranslate">mpl.rcParams['figure.constrained_layout.use'] = True</code>, but call <code class="notranslate">subplots</code> with <code class="notranslate">layout = 'constrained'</code>, the saved figure is as expected:<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/64837956/194764346-8b12b2f5-44f8-4ba5-8440-2fd5d2581cb1.png"><img src="https://user-images.githubusercontent.com/64837956/194764346-8b12b2f5-44f8-4ba5-8440-2fd5d2581cb1.png" alt="test" style="max-width: 100%;"></a></p>
<p dir="auto">(The deprecation warning is probably the same is in <a href="https://github.com/matplotlib/matplotlib/issues/23921" data-hovercard-type="issue" data-hovercard-url="/matplotlib/matplotlib/issues/23921/hovercard">#23921</a>, and appears in both cases.)</p>
<h3 dir="auto">Additional information</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Operating system</h3>
<p dir="auto">macOS Monterey</p>
<h3 dir="auto">Matplotlib Version</h3>
<p dir="auto">3.6.0</p>
<h3 dir="auto">Matplotlib Backend</h3>
<p dir="auto">macOSX</p>
<h3 dir="auto">Python version</h3>
<p dir="auto">3.10.6</p>
<h3 dir="auto">Jupyter version</h3>
<p dir="auto"><em>No response</em></p>
<h3 dir="auto">Installation</h3>
<p dir="auto">pip</p>