Skip to content

Add optional axes-aware text wrapping#31633

Closed
codegnan-dm wants to merge 5 commits into
matplotlib:mainfrom
codegnan-dm:fix/text-wrap-axes-boundary
Closed

Add optional axes-aware text wrapping#31633
codegnan-dm wants to merge 5 commits into
matplotlib:mainfrom
codegnan-dm:fix/text-wrap-axes-boundary

Conversation

@codegnan-dm

@codegnan-dm codegnan-dm commented May 8, 2026

Copy link
Copy Markdown
Screenshot 2026-05-08 111451 Screenshot 2026-05-08 112557 Fixes #31635

Summary

When wrap=True is used on text inside an axes, the wrapping incorrectly
used the figure boundary instead of the axes boundary. This caused text
to overflow outside its axes into adjacent axes or outside the figure.

This fix checks whether the text object belongs to an axes, and if so,
uses the axes boundary for wrapping. Otherwise it falls back to the
figure boundary.

Fixes

Closes #[correct issue number here]

PR Checklist

  • Has pytest style unit tests
  • Code is well-commented
  • New features/changed behavior has been documented

AI Disclosure

This PR was developed with AI assistance for guidance on code location
and fix approach. The implementation, testing, and verification were
done manually.

@codegnan-dm codegnan-dm force-pushed the fix/text-wrap-axes-boundary branch from 14d1973 to d3cadcb Compare May 8, 2026 06:13
@codegnan-dm codegnan-dm changed the title Fix text wrapping to use axes boundary instead of figure boundary\ Fix text wrapping to use axes boundary instead of figure boundary May 8, 2026
@codegnan-dm codegnan-dm force-pushed the fix/text-wrap-axes-boundary branch from d3cadcb to b665ac4 Compare May 8, 2026 06:17
@rcomer

rcomer commented May 8, 2026

Copy link
Copy Markdown
Member

Thank you for your willingness to contribute. Did you intend to link a different issue? The one you linked is not about text in an axes.

Also please fill out the PR summary using our template and ensure the tests pass.

@rcomer rcomer added status: needs clarification Issues that need more information to resolve. status: autoclose candidate PRs that are not yet ready for review and may be automatically closed in two weeks labels May 8, 2026
@github-actions

github-actions Bot commented May 8, 2026

Copy link
Copy Markdown

⏰ This pull request might be automatically closed in two weeks from now.

Thank you for your contribution to Matplotlib and for the effort you have put into this PR. This pull request does not yet meet the quality and clarity standards needed for an effective review. Project maintainers have limited time for code reviews, and our goal is to prioritize well-prepared contributions to keep Matplotlib maintainable.

Matplotlib maintainers cannot provide one-to-one guidance on this PR. However, if you ask focused, well-researched questions, a community member may be willing to help. 💬

To increase the chance of a productive review:

As the author, you are responsible for driving this PR, which entails doing necessary background research as well as presenting its context and your thought process. If you are a new contributor, or do not know how to fulfill these requirements, we recommend that you familiarize yourself with Matplotlib's development conventions or engage with the community via our Discourse or one of our meetings before submitting code.

If you substantially improve this PR within two weeks, leave a comment and a team member may remove the status: autoclose candidate label and the PR stays open. Cosmetic changes or incomplete fixes will not be sufficient. Maintainers will assess improvements on their own schedule. Please do not ping (@) maintainers.

@codegnan-dm

Copy link
Copy Markdown
Author

Thanks for the feedback.

I’ll update the PR summary/template and verify whether the linked issue is correct.

I’ll also investigate the failing tests and push fixes shortly.

@codegnan-dm

Copy link
Copy Markdown
Author

@rcomer Thank you for the clarification. I have opened a new issue #31635
which correctly describes the bug this PR fixes — when ax.text()
is used with wrap=True inside an axes, wrapping incorrectly uses
the figure boundary instead of the axes boundary, causing text to
overflow outside the axes. I have updated the PR description accordingly.

@codegnan-dm

Copy link
Copy Markdown
Author

The rstcheck precommit failure is a pre-existing issue in the repo —
doc/users/getting_started/index.rst references a file
/install/quick_install.inc.rst that does not exist. This is unrelated
to my changes and can be verified by checking the same error on other
recent PRs.

@codegnan-dm

Copy link
Copy Markdown
Author

The AppVeyor Windows CI failure is an infrastructure issue — micromamba
failed to parse its history file and crashed during dependency installation
(line 1929: Command exited with code 1). Tests never ran as a result.
This is unrelated to my changes.

@codegnan-dm codegnan-dm changed the title Fix text wrapping to use axes boundary instead of figure boundary Add optional axes-aware text wrapping May 8, 2026
@codegnan-dm

Copy link
Copy Markdown
Author

@rcomer Thanks for the clarification.

I understand the current behavior is consistent with the documented implementation of set_wrap.

I’ve updated the PR to frame this as an optional axes-aware wrapping enhancement while preserving existing default behavior for backward compatibility.

@rcomer

rcomer commented May 8, 2026

Copy link
Copy Markdown
Member

@codegnan-dm before taking this PR further, please could you open an issue feature request describing your use case? I.e., what do you use Matplotlib for and why do you want this?

@rcomer rcomer removed the status: autoclose candidate PRs that are not yet ready for review and may be automatically closed in two weeks label May 8, 2026
@codegnan-dm

Copy link
Copy Markdown
Author

@rcomer I have opened a feature request issue #31655 describing my use case.

I use matplotlib for creating multi-axes figures such as dashboards and
subplot-based reports. When adding long text annotations inside narrow axes
using ax.text() with wrap=True, the text overflows into adjacent axes because
wrapping uses the figure boundary instead of the axes boundary.

The fix in this PR adds optional axes-aware wrapping — when text belongs to
an axes, it wraps within the axes boundary. When text is directly on a figure
(e.g. suptitle), it falls back to the figure boundary as before. This preserves
full backward compatibility.

Issue: #31655

@timhoffm timhoffm left a comment

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.

You apparently have a code formatter on that reformats large parts of otherwise untouched code. Please revert these changes.

Image test shows that some cases do not behave nicely. In particular why does the top centered text wrap after every word. I wouldn't expect that. I haven't analyzed the bottom left case, but suspect that it does not have enough space due to rotation. The over-constraint will make it wrap after every word. Not sure whether we can have better fallbacks.

Overall, since we are regenerating the test images, we could improve on the tests. Each text should catch a specific case, and it would be good if the respective text directly describes this case instead of using a generic text. Check whether relevant cases are sufficiently covered or whether some texts just test the same thing.

We may also consider using text placeholders as introduced in #29872 to reduce the dependency on text rendering details.

@rcomer

rcomer commented May 19, 2026

Copy link
Copy Markdown
Member

In particular why does the top centered text wrap after every word.

I suspect that needs #31546, but I haven't checked.

I haven't analyzed the bottom left case

That one is positioned outside the axes at (-1, 0). I do not think wrapping to the axes makes sense in that case.

@rcomer

rcomer commented May 19, 2026

Copy link
Copy Markdown
Member

I think this should get a test for axes title as I think that would be a very common use case that is currently broken by this PR.

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(3, 3), layout='constrained')
ax.set_title('axes title that is too long for the size', wrap=True)

plt.show()

main
main

this PR
branch

@rcomer rcomer removed the status: needs clarification Issues that need more information to resolve. label May 19, 2026
@codegnan-dm

codegnan-dm commented May 20, 2026

Copy link
Copy Markdown
Author

Superseded by #31716 which implements the design agreed upon in issue #31655 — supporting wrap='axes' and wrap='figure' as string values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request]: Support wrapping text relative to axes boundaries

3 participants