Skip to content
Closed
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
25 changes: 18 additions & 7 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,12 +709,14 @@ def set_wrap(self, wrap):
"""
Set whether the text can be wrapped.

Wrapping makes sure the text is confined to the (sub)figure box. It
does not take into account any other artists.
Wrapping makes sure the text is confined to the (sub)figure or axes
box. It does not take into account any other artists.

Parameters
----------
wrap : bool
wrap : bool or {'axes', 'figure'}
- True or 'figure': wrap to the figure boundary (default)
- 'axes': wrap to the axes boundary

Notes
-----
Expand All @@ -724,6 +726,10 @@ def set_wrap(self, wrap):
rescales the canvas to accommodate all content and happens before
wrapping.
"""
if wrap not in (True, False, 'axes', 'figure'):
raise ValueError(
f"wrap must be True, False, 'axes', or 'figure', "
f"got {wrap!r}")
self._wrap = wrap

def _get_wrap_line_width(self):
Expand All @@ -732,16 +738,21 @@ def _get_wrap_line_width(self):
orientation.
"""
x0, y0 = self.get_transform().transform(self.get_position())
figure_box = self.get_figure().get_window_extent()

# Determine clip boundary based on wrap setting
if self.get_wrap() == 'axes' and self.axes is not None:
clip_box = self.axes.get_window_extent()
else:
clip_box = self.get_figure().get_window_extent()

# Calculate available width based on text alignment
alignment = self.get_horizontalalignment()
self.set_rotation_mode('anchor')
angle = self.get_rotation()

left = self._get_dist_to_box(angle, x0, y0, figure_box)
left = self._get_dist_to_box(angle, x0, y0, clip_box)
right = self._get_dist_to_box(
(180 + angle) % 360, x0, y0, figure_box)
(180 + angle) % 360, x0, y0, clip_box)

if alignment == 'left':
line_width = left
Expand Down Expand Up @@ -791,7 +802,7 @@ def _get_wrapped_text(self):
Return a copy of the text string with new lines added so that the text
is wrapped relative to the parent figure (if `get_wrap` is True).
"""
if not self.get_wrap():
if self._wrap is False:
return self.get_text()

# Not fit to handle breaking up latex syntax correctly, so
Expand Down
Loading