Skip to content

[ENH]: Optional axes-aware text wrapping for wrap=True #31655

@codegnan-dm

Description

@codegnan-dm

Problem

When creating multi-axes figures with subplots (e.g. dashboards,
reports, side-by-side comparisons), using ax.text() with wrap=True
causes text to wrap based on the figure boundary instead of the axes
boundary. This means text overflows outside its axes into adjacent
axes or outside the figure entirely, making the plot look broken.

Example: A 2-column subplot figure where a long annotation in the
left axes spills into the right axes area.

Proposed solution

Add an option for wrap=True to respect the axes boundary when text
is placed inside an axes, so annotations stay visually contained
within their axes.

This could be implemented by checking whether the text object belongs
to an axes (self.axes is not None) in the _get_wrap_line_width()
method of text.py, and using the axes boundary instead of the figure
boundary when calculating the available wrap width.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions