GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — TST: Use placeholders for text in layout tests by QuLogic · Pull Request #29872 · matplotlib/matplotlib

Conversation

@QuLogic

@QuLogic QuLogic commented Apr 5, 2025

Copy link
Copy Markdown
Member

PR summary

This is an alternative to #29833 which patches the Text object to draw a rectangle and not depend on any properties of the actual font. The calculation for the placeholders is somewhat arbitrary, but actually ends up rather close to the size of the original text, so most images don't change a whole lot.

I tested this by changing matplotlib.testing.set_font_settings_for_testing from DejaVu Sans to Noto Sans; this would have failed most of the tests before, but none failed now.

PR checklist

  • "closes #0000" is in the body of the PR description to link the related issue
  • new and changed code is tested
  • [n/a] Plotting related features are demonstrated in an example
  • [n/a] New Features and API Changes are noted with a directive and release note
  • [n/a] Documentation complies with general and docstring guidelines

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@QuLogic QuLogic added topic: geometry manager LayoutEngine, Constrained layout, Tight layout topic: testing labels Apr 5, 2025
@github-actions github-actions Bot removed the topic: geometry manager LayoutEngine, Constrained layout, Tight layout label Apr 5, 2025
QuLogic
QuLogic commented Apr 5, 2025
View reviewed changes
Comment thread lib/matplotlib/tests/test_constrainedlayout.py

pos = axs[0, 0].get_position()
np.testing.assert_allclose(pos.x0, 0.2344, atol=1e-3)
np.testing.assert_allclose(pos.x0, 0.2381, atol=1e-2)

@QuLogic QuLogic Apr 5, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Note, I found test_compressed1 to be a bit flaky. Out of 200 repeats, 12 failed on this line or one of the two below where I changed the tolerance. I'm not entirely sure why, as the other tests don't have an issue. If you prefer, I can change the tolerance on all of these lines for consistency.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@QuLogic
QuLogic force-pushed the layout-text-placeholders branch from 81dfd4b to de67704 Compare April 5, 2025 07:23
@QuLogic QuLogic mentioned this pull request Apr 5, 2025
ENH: Add option to relpace text with boxes in image_comparision test #29871
Closed
timhoffm
timhoffm reviewed Apr 5, 2025
View reviewed changes
Comment thread lib/matplotlib/testing/conftest.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

jklymak
jklymak approved these changes Apr 5, 2025
View reviewed changes

@jklymak jklymak left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Looks good. I would never have known to use monkeypatch!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@jklymak jklymak mentioned this pull request Apr 5, 2025
TST: remove (most) text from constrained layout tests #29833
Closed
timhoffm
timhoffm previously requested changes Apr 7, 2025
View reviewed changes
Comment thread lib/matplotlib/testing/conftest.py Outdated
if self.get_text() == '':
return
bbox = self.get_window_extent()
Rectangle(bbox.p0, bbox.width, bbox.height, color='grey').draw(renderer)

@timhoffm timhoffm Apr 7, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Suggested change
Rectangle(bbox.p0, bbox.width, bbox.height, color='grey').draw(renderer)
Rectangle(bbox.p0, bbox.width, bbox.height, facecolor='grey').draw(renderer)

color would also set the edgecolor, which is transparent by default ('none'). The edgecolor induces a line to be drawn around the region and since lines have antialiasing, the edges can become blury. We don't want that. See #29874 (comment), where the same effect shows up in a different context.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@QuLogic
QuLogic force-pushed the layout-text-placeholders branch from de67704 to 35442a2 Compare April 8, 2025 00:22
@QuLogic

QuLogic commented Apr 8, 2025

Copy link
Copy Markdown
Member Author

I pushed a couple minor tweaks:

  1. Changed the box to get its colour from the text. This isn't used in these tests, but could be elsewhere.
  2. Removed the edge colour from the box.
  3. Added a comment about the bounding box size.
  4. Tweaked the importing so that there's only one fixture.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@timhoffm
timhoffm dismissed their stale review April 8, 2025 02:13

Edge color removed

timhoffm
timhoffm reviewed Apr 8, 2025
View reviewed changes
Comment thread lib/matplotlib/testing/conftest.py Outdated
return
bbox = self.get_window_extent()
rect = Rectangle(bbox.p0, bbox.width, bbox.height,
facecolor=(self.get_color(), 0.5), edgecolor='none')

@timhoffm timhoffm Apr 8, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I'm a bit torn on using transparency here. On the one hand, that correctly catches overlay (but that should be rare). On the other hand, alpha blending is an additional component determining the output image, can that lead to more unstable reference images in the future?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@QuLogic QuLogic Apr 8, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Fair enough. I think it should only make a difference if there is overlap and the z-order changes, but that's not important enough to check here.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

timhoffm reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@timhoffm timhoffm Apr 8, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Test images looks a bit redacted now 😆, but I think this is better.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@QuLogic
QuLogic force-pushed the layout-text-placeholders branch from 35442a2 to 60c293f Compare April 8, 2025 05:54
@github-actions github-actions Bot added the topic: mpl_toolkit label Apr 8, 2025
@QuLogic

QuLogic commented Apr 8, 2025

Copy link
Copy Markdown
Member Author

I made one more change to 2 tests that I found in #29816 that are about tight layout, namely that calling fig.tight_layout() within the test, with image_comparison(..., remove_text=True) does not layout correctly. The text would be removed after the layout, and thus the test image was still beholden to text properties.

I fixed this by setting the layout on the Figure itself, so it would be enabled on save and correctly ignore text. Also, I changed the table test to use placeholders.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@QuLogic
QuLogic force-pushed the layout-text-placeholders branch 2 times, most recently from 37e5518 to 064c10e Compare April 8, 2025 10:20
timhoffm
timhoffm reviewed Apr 8, 2025
View reviewed changes

@timhoffm timhoffm left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This should also be mentioned in

https://matplotlib.org/devdocs/devel/testing.html#writing-an-image-comparison-test

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread lib/matplotlib/testing/conftest.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@QuLogic
QuLogic force-pushed the layout-text-placeholders branch from d46840e to 7c466f9 Compare April 9, 2025 07:03
@github-actions github-actions Bot added the Documentation: devdocs files in doc/devel label Apr 9, 2025
@QuLogic

QuLogic commented Apr 9, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

While updating that doc section, I was also reminded that we should prefer mpl20 style. I guess I should update all of these, though it would change a bunch of figure sizes and also where the ticks are, so I'm not sure if I should for the layouting tests. That change would be here: https://github.com/QuLogic/matplotlib/compare/layout-text-placeholders..ltp

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@timhoffm

timhoffm commented Apr 9, 2025

Copy link
Copy Markdown
Member

I would do the mpl20 change in one go. I checked the diffs: The smaller figure sizes are ok. AFAICS no layout information is lost. The only one I'm not sure about is tight_layout_offsetboxes2.svg

image

The offset boxes (red/green) and the labels of the right column overlap. But I guess that's just how tightlayout works. It does not account for the actual label sizes.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jklymak

jklymak commented Apr 9, 2025

Copy link
Copy Markdown
Member

Tight_layout should account for the actual reported label sizes.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@timhoffm

timhoffm commented Apr 9, 2025

Copy link
Copy Markdown
Member

So is this an issue due the rectangle not representing the actual text width but rather an approximation?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jklymak

jklymak commented Apr 9, 2025

Copy link
Copy Markdown
Member

I'm not sure - I dont' see an equivalent test for constrained layout - but maybe it is a draw twice sort of error, where the offset boxes get moved when the axes gets resized, and therefore the margin made for the axes is too small for th new position. We run constrained layout twice to get around that; usually the position is "closer" after the first time and the new margin is big enough.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@timhoffm

timhoffm commented Apr 9, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Hm @QuLogic maybe the workaround here could be to increase the figure size to not be bothered with this topic right now? 🤯 Not great, but digging into whether this is a tightlayout issue or a box approximation issue also does not feel like a good use of time. And I don't like the present image with the overlap.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@QuLogic

QuLogic commented Apr 10, 2025

Copy link
Copy Markdown
Member Author

Tight_layout should account for the actual reported label sizes.

But these are not labels, they are AnchoredOffsetBox. Trying it out with constrained layout, it also doesn't seem to interact with the ticks either.

Based on the comment in the test, it appears the intention is to ensure that the boxes are included in the Axes bounding box, but not necessarily have any effect on the ticks labels. Then the real test is the difference between 1 and 2, where the invisible AnchoredOffsetBox don't count for the bounding box in the latter, and it looks like that property is preserved.

So I think that a) we can drop this test down to just .png since it's really just checking if a specific artist is included, b) maybe we should remove the tick labels entirely to avoid confusing ourselves, and c) maybe this doesn't need to be an image test at all?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jklymak

jklymak commented Apr 10, 2025

Copy link
Copy Markdown
Member

Oh I see. Yes the boxes can overlap the labels in their own axes. They should not overlap labels in other axes.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@QuLogic

QuLogic commented Apr 10, 2025

Copy link
Copy Markdown
Member Author

Here's my suggestion for removing the image from the test: b6dff32

It creates 3 figures:

  1. one without any AnchoredOffsetbox
  2. one with AnchoredOffsetbox around each Axes
  3. one with them invisible AnchoredOffsetbox

Then the test is that in figure 2, each Axes is smaller than in figure 1, and no boxes overlap. And then for figure 3, it's that it is the same as figure 1.

If that makes some sense, then I'll rebase this PR on top of that, so we can drop the figures before changing them here.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tacaswell tacaswell added this to the v3.10.2 milestone Apr 10, 2025
QuLogic and others added 4 commits April 10, 2025 14:51
@QuLogic
TST: Remove image from offsetbox tight layout test
ed9a60b
@QuLogic
TST: Use placeholders for text in layout tests
4ae4e08
@QuLogic
TST: Fix tight layout tests without text
44cda68
If we pass `remove_text` to `image_comparison`, we don't expect tests to
change from FreeType. But if `tight_layout` is called *before* the end
of the test function, the layout will happen with the text that was
supposed to be removed.
@QuLogic @timhoffm
Document the text_placeholders fixture
3d0fd3c
Co-authored-by: Tim Hoffmann <[email protected]>
@tacaswell

tacaswell commented Apr 10, 2025

Copy link
Copy Markdown
Member

I am 👍 on moving to the version of that test that @QuLogic has proposed and moving all of these to mpl20 style in this PR.

I think that this is the next PR in the sequence to move to freetype 2.13

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@QuLogic
QuLogic force-pushed the layout-text-placeholders branch from 7c466f9 to 3d0fd3c Compare April 10, 2025 18:55
timhoffm
timhoffm approved these changes Apr 10, 2025
View reviewed changes
@timhoffm
timhoffm merged commit 4658ab0 into matplotlib:main Apr 10, 2025
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Apr 10, 2025
@timhoffm @meeseeksmachine
Backport PR matplotlib#29872: TST: Use placeholders for text in layou…
e7bfa99
…t tests
@meeseeksmachine meeseeksmachine mentioned this pull request Apr 10, 2025
Backport PR #29872 on branch v3.10.x (TST: Use placeholders for text in layout tests) #29901
Merged
@QuLogic
QuLogic deleted the layout-text-placeholders branch April 10, 2025 22:31
@QuLogic QuLogic mentioned this pull request Apr 12, 2025
Ensure text metric calculation always uses the text cache #29907
Merged
5 tasks
rcomer pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Apr 20, 2025
@timhoffm @rcomer
Backport PR matplotlib#29872: TST: Use placeholders for text in layou…
d1c20ea
…t tests
rcomer added a commit that referenced this pull request Apr 20, 2025
@rcomer
Merge pull request #29901 from meeseeksmachine/auto-backport-of-pr-29…
110b6af
…872-on-v3.10.x

Backport PR #29872 on branch v3.10.x (TST: Use placeholders for text in layout tests)
@ksunden ksunden mentioned this pull request May 9, 2025
merge up 3.10.3 #30031
Closed
5 tasks
@QuLogic QuLogic added this to Font and text overhaul Jun 5, 2025
@QuLogic QuLogic moved this to Done in Font and text overhaul Jun 5, 2025
@ksunden ksunden mentioned this pull request Jul 25, 2025
Manual Backport PR #30195 on branch v3.10.x (ci: Enable wheel builds on Python 3.14) #30356
Merged
5 tasks
@timhoffm timhoffm mentioned this pull request Aug 1, 2025
TST: Use shapes instead of actual text for layout manager tests #29825
Closed
@ksunden ksunden mentioned this pull request Aug 7, 2025
merge up v3.10.5 #30401
Merged
5 tasks
@QuLogic QuLogic mentioned this pull request Sep 17, 2025
Drop the FT2Font intermediate buffer. #30059
Merged
5 tasks
@timhoffm timhoffm mentioned this pull request May 19, 2026
Add optional axes-aware text wrapping #31633
Closed
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jklymak jklymak jklymak approved these changes
@timhoffm timhoffm timhoffm approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

Documentation: devdocs files in doc/devel topic: mpl_toolkit topic: testing

Projects

Font and text overhaul
Status: Done

Milestone

v3.10.3

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@QuLogic @timhoffm @jklymak @tacaswell
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.