Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cc85130

Browse filesBrowse 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 matplotlib#28378 Closes matplotlib#28358
1 parent 8b9cb14 commit cc85130
Copy full SHA for cc85130

File tree

2 files changed

+13
-9
lines changed
Filter options

2 files changed

+13
-9
lines changed

‎lib/matplotlib/tests/test_text.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_text.py
+7-2Lines changed: 7 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,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

Copy file name to clipboardExpand all lines: lib/matplotlib/text.py
+6-7Lines changed: 6 additions & 7 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.