Skip to content

Commit

Permalink
FIX: Fix text wrapping
Browse files Browse the repository at this point in the history
`_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
  • Loading branch information
timhoffm committed Jun 15, 2024
1 parent 8b9cb14 commit 29ef716
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
10 changes: 8 additions & 2 deletions lib/matplotlib/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from matplotlib.font_manager import FontProperties
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import matplotlib.transforms as mtransforms
from matplotlib.testing.decorators import check_figures_equal, image_comparison
from matplotlib.testing._markers import needs_usetex
Expand Down Expand Up @@ -707,9 +708,14 @@ def test_large_subscript_title():
(0.3, 0, 'right'),
(0.3, 185, 'left')])
def test_wrap(x, rotation, halign):
fig = plt.figure(figsize=(6, 6))
fig = plt.figure(figsize=(18, 18))
gs = GridSpec(nrows=3, ncols=3, figure=fig,
left=0, right=1, bottom=0, top=1)
subfig = fig.add_subfigure(gs[1, 1])
# we only use the central subfigure, which does not align with any
# figure boundary, to ensure only subfigure boundaries are relevant
s = 'This is a very long text that should be wrapped multiple times.'
text = fig.text(x, 0.7, s, wrap=True, rotation=rotation, ha=halign)
text = subfig.text(x, 0.7, s, wrap=True, rotation=rotation, ha=halign)
fig.canvas.draw()
assert text._get_wrapped_text() == ('This is a very long\n'
'text that should be\n'
Expand Down
8 changes: 4 additions & 4 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,16 +657,16 @@ def _get_dist_to_box(self, rotation, x0, y0, figure_box):
"""
if rotation > 270:
quad = rotation - 270
h1 = y0 / math.cos(math.radians(quad))
h1 = (y0 - figure_box.y0) / math.cos(math.radians(quad))
h2 = (figure_box.x1 - x0) / math.cos(math.radians(90 - quad))
elif rotation > 180:
quad = rotation - 180
h1 = x0 / math.cos(math.radians(quad))
h2 = y0 / math.cos(math.radians(90 - quad))
h1 = (x0 - figure_box.x0) / math.cos(math.radians(quad))
h2 = (y0 - figure_box.y0) / math.cos(math.radians(90 - quad))
elif rotation > 90:
quad = rotation - 90
h1 = (figure_box.y1 - y0) / math.cos(math.radians(quad))
h2 = x0 / math.cos(math.radians(90 - quad))
h2 = (x0 - figure_box.x0) / math.cos(math.radians(90 - quad))
else:
h1 = (figure_box.x1 - x0) / math.cos(math.radians(rotation))
h2 = (figure_box.y1 - y0) / math.cos(math.radians(90 - rotation))
Expand Down

0 comments on commit 29ef716

Please sign in to comment.