Skip to content

Fix confusion between text height and ascent in metrics calculations.#31107

Open
anntzer wants to merge 1 commit intomatplotlib:text-overhaulfrom
anntzer:supal
Open

Fix confusion between text height and ascent in metrics calculations.#31107
anntzer wants to merge 1 commit intomatplotlib:text-overhaulfrom
anntzer:supal

Conversation

@anntzer
Copy link
Contributor

@anntzer anntzer commented Feb 6, 2026

The text height includes both the ascender and the descender, but the logic in _get_layout is that multiline texts should be treated as having an ascent at least as large as "l" and a descent at least as large as "p" (not a height at least as large as "lp" and a descent at least as large as "p") to prevent lines from bumping into each other (see changes to test_text/test_multiline, where the topmost superscript was close to bumping into the "p" descender previously).

Partial fix for #21653; see #21653 (comment).

old
multiline-expected
new
multiline

PR summary

PR checklist

xys = M.transform(offset_layout) - (offsetx, offsety)

return bbox, list(zip(lines, zip(ws, hs), *xys.T)), descent
return bbox, list(zip(lines, wads, *xys.T)), descent
Copy link
Member

Choose a reason for hiding this comment

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

Do we still need to return descent now that we have d in wads?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, it was anyways unused; removed.

@timhoffm
Copy link
Member

timhoffm commented Feb 6, 2026

You need to also update this call:

bbox, info, yd = self._text._get_layout(renderer)

yd is not unused here. You must reconstruct it from wads.

@anntzer
Copy link
Contributor Author

anntzer commented Feb 6, 2026

Oops, I missed that call site; fixed.

@timhoffm
Copy link
Member

timhoffm commented Feb 6, 2026

Code looks good. Not checked, but I assume the image comparison failures are from slight text changes? What's the plan to handle them?

@QuLogic QuLogic moved this to Ready for Review in Font and text overhaul Feb 6, 2026
@QuLogic QuLogic added this to the v3.11.0 milestone Feb 6, 2026
@QuLogic
Copy link
Member

QuLogic commented Feb 7, 2026

I don't see anything obviously wrong in the code so far. This PR targets the text-overhaul branch, so test images can be handled as with all the other text changes.

There are 248 test image failures with this change, but they are a bit inconsistent in how they manifest. For example, in test_axes.py::test_errorbar[png], the log (10^x) ticks all shift up. I'm uncertain whether any of these ticks are supposed to align here with the linear axis in the other Axes but at least the top and bottom one are close, and they went from about 1 pixel lower to 0.5-1 pixel higher. On the other hand, in test_image.py::test_nonuniform_logscale[png] the log ticks all shift down one pixel. Other than the multiline text alignment image in the original post, maybe the legend in the patheffects tests is the most noticeable change.

@anntzer
Copy link
Contributor Author

anntzer commented Feb 7, 2026

Agreed that it is hard to judge whether most changes (in non-multiline text) are really better or worse, except the patheffect legend (test_patheffect3) which appears clearly better (more vertically centered in the box) to me.

old
patheffect3-expected
new
patheffect3
(it's much easier to see the difference with the builtin triager, though).

@tacaswell
Copy link
Member

My read is that this makes the code match what the comments said it actually did.

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

Looks good apart from the vauge naming.

The text height includes both the ascender and the descender, but the
logic in _get_layout is that multiline texts should be treated as having
an ascent at least as large as "l" and a descent at least as large as
"p" (not a height at least as large as "lp" and a descent at least as
large as "p") to prevent lines from bumping into each other (see changes
to test_text/test_multiline, where the topmost superscript was close to
bumping into the "p" descender previously).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Ready for Review

Development

Successfully merging this pull request may close these issues.

4 participants