Skip to content

Commit f8f8206

Browse files
timhoffmmeeseeksmachine
authored andcommitted
Backport PR matplotlib#28401: FIX: Fix text wrapping
1 parent 40f7f09 commit f8f8206

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

lib/matplotlib/tests/test_text.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from matplotlib.font_manager import FontProperties
1616
import matplotlib.patches as mpatches
1717
import matplotlib.pyplot as plt
18+
from matplotlib.gridspec import GridSpec
1819
import matplotlib.transforms as mtransforms
1920
from matplotlib.testing.decorators import check_figures_equal, image_comparison
2021
from matplotlib.testing._markers import needs_usetex
@@ -707,9 +708,13 @@ def test_large_subscript_title():
707708
(0.3, 0, 'right'),
708709
(0.3, 185, 'left')])
709710
def test_wrap(x, rotation, halign):
710-
fig = plt.figure(figsize=(6, 6))
711+
fig = plt.figure(figsize=(18, 18))
712+
gs = GridSpec(nrows=3, ncols=3, figure=fig)
713+
subfig = fig.add_subfigure(gs[1, 1])
714+
# we only use the central subfigure, which does not align with any
715+
# figure boundary, to ensure only subfigure boundaries are relevant
711716
s = 'This is a very long text that should be wrapped multiple times.'
712-
text = fig.text(x, 0.7, s, wrap=True, rotation=rotation, ha=halign)
717+
text = subfig.text(x, 0.7, s, wrap=True, rotation=rotation, ha=halign)
713718
fig.canvas.draw()
714719
assert text._get_wrapped_text() == ('This is a very long\n'
715720
'text that should be\n'

lib/matplotlib/text.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -606,9 +606,8 @@ def set_wrap(self, wrap):
606606
"""
607607
Set whether the text can be wrapped.
608608
609-
Wrapping makes sure the text is completely within the figure box, i.e.
610-
it does not extend beyond the drawing area. It does not take into
611-
account any other artists.
609+
Wrapping makes sure the text is confined to the (sub)figure box. It
610+
does not take into account any other artists.
612611
613612
Parameters
614613
----------
@@ -657,16 +656,16 @@ def _get_dist_to_box(self, rotation, x0, y0, figure_box):
657656
"""
658657
if rotation > 270:
659658
quad = rotation - 270
660-
h1 = y0 / math.cos(math.radians(quad))
659+
h1 = (y0 - figure_box.y0) / math.cos(math.radians(quad))
661660
h2 = (figure_box.x1 - x0) / math.cos(math.radians(90 - quad))
662661
elif rotation > 180:
663662
quad = rotation - 180
664-
h1 = x0 / math.cos(math.radians(quad))
665-
h2 = y0 / math.cos(math.radians(90 - quad))
663+
h1 = (x0 - figure_box.x0) / math.cos(math.radians(quad))
664+
h2 = (y0 - figure_box.y0) / math.cos(math.radians(90 - quad))
666665
elif rotation > 90:
667666
quad = rotation - 90
668667
h1 = (figure_box.y1 - y0) / math.cos(math.radians(quad))
669-
h2 = x0 / math.cos(math.radians(90 - quad))
668+
h2 = (x0 - figure_box.x0) / math.cos(math.radians(90 - quad))
670669
else:
671670
h1 = (figure_box.x1 - x0) / math.cos(math.radians(rotation))
672671
h2 = (figure_box.y1 - y0) / math.cos(math.radians(90 - rotation))

0 commit comments

Comments
 (0)