Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29ef716

Browse files
committed
FIX: Fix text wrapping
`_get_dist_to_box()` assumed that the figure box (x0, y0) coordinates are 0, which was correct at the time of writing, but does not hold anymore since the introduction of subfigures. Closes #28378 Closes #28358
1 parent 8b9cb14 commit 29ef716

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

lib/matplotlib/tests/test_text.py

Lines changed: 8 additions & 2 deletions
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,14 @@ 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+
left=0, right=1, bottom=0, top=1)
714+
subfig = fig.add_subfigure(gs[1, 1])
715+
# we only use the central subfigure, which does not align with any
716+
# figure boundary, to ensure only subfigure boundaries are relevant
711717
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)
718+
text = subfig.text(x, 0.7, s, wrap=True, rotation=rotation, ha=halign)
713719
fig.canvas.draw()
714720
assert text._get_wrapped_text() == ('This is a very long\n'
715721
'text that should be\n'

lib/matplotlib/text.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,16 +657,16 @@ def _get_dist_to_box(self, rotation, x0, y0, figure_box):
657657
"""
658658
if rotation > 270:
659659
quad = rotation - 270
660-
h1 = y0 / math.cos(math.radians(quad))
660+
h1 = (y0 - figure_box.y0) / math.cos(math.radians(quad))
661661
h2 = (figure_box.x1 - x0) / math.cos(math.radians(90 - quad))
662662
elif rotation > 180:
663663
quad = rotation - 180
664-
h1 = x0 / math.cos(math.radians(quad))
665-
h2 = y0 / math.cos(math.radians(90 - quad))
664+
h1 = (x0 - figure_box.x0) / math.cos(math.radians(quad))
665+
h2 = (y0 - figure_box.y0) / math.cos(math.radians(90 - quad))
666666
elif rotation > 90:
667667
quad = rotation - 90
668668
h1 = (figure_box.y1 - y0) / math.cos(math.radians(quad))
669-
h2 = x0 / math.cos(math.radians(90 - quad))
669+
h2 = (x0 - figure_box.x0) / math.cos(math.radians(90 - quad))
670670
else:
671671
h1 = (figure_box.x1 - x0) / math.cos(math.radians(rotation))
672672
h2 = (figure_box.y1 - y0) / math.cos(math.radians(90 - rotation))

0 commit comments

Comments
 (0)