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

Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 53 additions & 17 deletions lib/matplotlib/_constrained_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,23 +440,59 @@

# make margins for figure-level legends:
for leg in fig.legends:
inv_trans_fig = None
if leg._outside_loc and leg._bbox_to_anchor is None:
if inv_trans_fig is None:
inv_trans_fig = fig.transFigure.inverted().transform_bbox
bbox = inv_trans_fig(leg.get_tightbbox(renderer))
w = bbox.width + 2 * w_pad
h = bbox.height + 2 * h_pad
legendloc = leg._outside_loc
if legendloc == 'lower':
layoutgrids[fig].edit_margin_min('bottom', h)
elif legendloc == 'upper':
layoutgrids[fig].edit_margin_min('top', h)
if legendloc == 'right':
layoutgrids[fig].edit_margin_min('right', w)
elif legendloc == 'left':
layoutgrids[fig].edit_margin_min('left', w)

# === NEW: handle figure-level texts ===
inv_trans_fig = fig.transFigure.inverted().transform_bbox

for text in fig.texts:

Check warning on line 446 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Undefined name `fig` Raw Output: message:"Undefined name `fig`" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/_constrained_layout.py" range:{start:{line:446 column:13} end:{line:446 column:16}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"F821" url:"https://docs.astral.sh/ruff/rules/undefined-name"}

Check failure on line 446 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / mypy

[mypy] reported by reviewdog 🐶 Name "fig" is not defined [name-defined] Raw Output: lib/matplotlib/_constrained_layout.py:446: error: Name "fig" is not defined [name-defined]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please test locally on a minimal script that shows the before and after before pushing to GitHub. There is no way this worked locally with the typo here and you can see you are failing all the tests. I'll move this to draft

Copy link
Copy Markdown
Author

@Arnav-xD Arnav-xD Mar 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out, I missed testing locally and introduced a typo. I'm trying to fix the issue and verify the behavior with a minimal example. I'll keep iterating if anything else needs adjustment.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you share the minimal example and before and after in the description of the PR? Also you appear to not have pushed any changes to the Github yet

if not text.get_visible() or not text.get_in_layout():
continue

try:
bbox = inv_trans_fig(text.get_tightbbox(renderer))

Check warning on line 451 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Undefined name `renderer` Raw Output: message:"Undefined name `renderer`" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/_constrained_layout.py" range:{start:{line:451 column:49} end:{line:451 column:57}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"F821" url:"https://docs.astral.sh/ruff/rules/undefined-name"}

Check warning on line 451 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Undefined name `inv_trans_fig` Raw Output: message:"Undefined name `inv_trans_fig`" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/_constrained_layout.py" range:{start:{line:451 column:16} end:{line:451 column:29}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"F821" url:"https://docs.astral.sh/ruff/rules/undefined-name"}

Check failure on line 451 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / mypy

[mypy] reported by reviewdog 🐶 Name "renderer" is not defined [name-defined] Raw Output: lib/matplotlib/_constrained_layout.py:451: error: Name "renderer" is not defined [name-defined]

Check failure on line 451 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / mypy

[mypy] reported by reviewdog 🐶 Name "inv_trans_fig" is not defined [name-defined] Raw Output: lib/matplotlib/_constrained_layout.py:451: error: Name "inv_trans_fig" is not defined [name-defined]
except Exception:
continue

if bbox is None:
continue

w = bbox.width + 2 * w_pad

Check warning on line 458 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Undefined name `w_pad` Raw Output: message:"Undefined name `w_pad`" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/_constrained_layout.py" range:{start:{line:458 column:26} end:{line:458 column:31}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"F821" url:"https://docs.astral.sh/ruff/rules/undefined-name"}

Check failure on line 458 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / mypy

[mypy] reported by reviewdog 🐶 Name "w_pad" is not defined [name-defined] Raw Output: lib/matplotlib/_constrained_layout.py:458: error: Name "w_pad" is not defined [name-defined]
h = bbox.height + 2 * h_pad

Check warning on line 459 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Undefined name `h_pad` Raw Output: message:"Undefined name `h_pad`" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/_constrained_layout.py" range:{start:{line:459 column:27} end:{line:459 column:32}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"F821" url:"https://docs.astral.sh/ruff/rules/undefined-name"}

Check failure on line 459 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / mypy

[mypy] reported by reviewdog 🐶 Name "h_pad" is not defined [name-defined] Raw Output: lib/matplotlib/_constrained_layout.py:459: error: Name "h_pad" is not defined [name-defined]

# Expand margins conservatively
if bbox.y1 > 1: # top overflow
layoutgrids[fig].edit_margin_min('top', h)

Check warning on line 463 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Undefined name `fig` Raw Output: message:"Undefined name `fig`" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/_constrained_layout.py" range:{start:{line:463 column:21} end:{line:463 column:24}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"F821" url:"https://docs.astral.sh/ruff/rules/undefined-name"}

Check warning on line 463 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Undefined name `layoutgrids` Raw Output: message:"Undefined name `layoutgrids`" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/_constrained_layout.py" range:{start:{line:463 column:9} end:{line:463 column:20}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"F821" url:"https://docs.astral.sh/ruff/rules/undefined-name"}

Check failure on line 463 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / mypy

[mypy] reported by reviewdog 🐶 Name "fig" is not defined [name-defined] Raw Output: lib/matplotlib/_constrained_layout.py:463: error: Name "fig" is not defined [name-defined]

Check failure on line 463 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / mypy

[mypy] reported by reviewdog 🐶 Name "layoutgrids" is not defined [name-defined] Raw Output: lib/matplotlib/_constrained_layout.py:463: error: Name "layoutgrids" is not defined [name-defined]
if bbox.y0 < 0: # bottom overflow
layoutgrids[fig].edit_margin_min('bottom', h)

Check warning on line 465 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Undefined name `fig` Raw Output: message:"Undefined name `fig`" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/_constrained_layout.py" range:{start:{line:465 column:21} end:{line:465 column:24}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"F821" url:"https://docs.astral.sh/ruff/rules/undefined-name"}

Check warning on line 465 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Undefined name `layoutgrids` Raw Output: message:"Undefined name `layoutgrids`" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/_constrained_layout.py" range:{start:{line:465 column:9} end:{line:465 column:20}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"F821" url:"https://docs.astral.sh/ruff/rules/undefined-name"}

Check failure on line 465 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / mypy

[mypy] reported by reviewdog 🐶 Name "fig" is not defined [name-defined] Raw Output: lib/matplotlib/_constrained_layout.py:465: error: Name "fig" is not defined [name-defined]

Check failure on line 465 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / mypy

[mypy] reported by reviewdog 🐶 Name "layoutgrids" is not defined [name-defined] Raw Output: lib/matplotlib/_constrained_layout.py:465: error: Name "layoutgrids" is not defined [name-defined]
if bbox.x0 < 0: # left overflow
layoutgrids[fig].edit_margin_min('left', w)

Check warning on line 467 in lib/matplotlib/_constrained_layout.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Undefined name `layoutgrids` Raw Output: message:"Undefined name `layoutgrids`" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/_constrained_layout.py" range:{start:{line:467 column:9} end:{line:467 column:20}}} severity:WARNING source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"F821" url:"https://docs.astral.sh/ruff/rules/undefined-name"}
if bbox.x1 > 1: # right overflow
layoutgrids[fig].edit_margin_min('right', w)


# === NEW: handle general figure legends (not just _outside_loc) ===
for leg in fig.legends:
if not leg.get_visible() or not leg.get_in_layout():
continue

try:
bbox = inv_trans_fig(leg.get_tightbbox(renderer))
except Exception:
continue

if bbox is None:
continue

w = bbox.width + 2 * w_pad
h = bbox.height + 2 * h_pad

if bbox.y1 > 1:
layoutgrids[fig].edit_margin_min('top', h)
if bbox.y0 < 0:
layoutgrids[fig].edit_margin_min('bottom', h)
if bbox.x0 < 0:
layoutgrids[fig].edit_margin_min('left', w)
if bbox.x1 > 1:
layoutgrids[fig].edit_margin_min('right', w)

def make_margin_suptitles(layoutgrids, fig, renderer, *, w_pad=0, h_pad=0):
# Figure out how large the suptitle is and make the
Expand Down
22 changes: 22 additions & 0 deletions lib/matplotlib/tests/test_constrainedlayout.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,30 @@
pytestmark = [
pytest.mark.usefixtures('text_placeholders')
]
def test_constrainedlayout_fig_text_included():
import matplotlib.pyplot as plt

fig, ax = plt.subplots(layout="constrained")
fig.text(0.5, 0.98, "Figure Title", ha="center")

fig.draw_without_rendering()

# Ensure no overlap: text should be above axes
ax_bbox = ax.get_position()
assert ax_bbox.y1 < 0.98

def test_constrainedlayout_fig_legend_included():
import matplotlib.pyplot as plt

fig, ax = plt.subplots(layout="constrained")
ax.plot([1, 2, 3], label="line")
fig.legend(loc="upper center")

fig.draw_without_rendering()

ax_bbox = ax.get_position()
assert ax_bbox.y1 < 1.0 # leaves space for legend

def example_plot(ax, fontsize=12, nodec=False):
ax.plot([1, 2])
ax.locator_params(nbins=3)
Expand Down
Loading