Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 14 additions & 4 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,20 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
xo, yo = font.get_bitmap_offset()
xo /= 64.0
yo /= 64.0
xd = d * sin(radians(angle))
yd = d * cos(radians(angle))
x = round(x + xo + xd)
y = round(y + yo + yd)

rad = radians(angle)
xd = d * sin(rad)
yd = d * cos(rad)
# Rotating the offset vector ensures text rotates around the anchor point.
# Without this, rotated text offsets incorrectly, causing a horizontal shift.
# Applying the 2D rotation matrix.
rotated_xo = xo * cos(rad) - yo * sin(rad)
rotated_yo = xo * sin(rad) + yo * cos(rad)
# Subtract rotated_yo to account for the inverted y-axis in computer graphics,
# compared to the mathematical convention.
x = round(x + rotated_xo + xd)
y = round(y - rotated_yo + yd)

self._renderer.draw_text_image(font, x, y + 1, angle, gc)

def get_text_width_height_descent(self, s, prop, ismath):
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions lib/matplotlib/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,22 @@ def test_alignment():
ax.set_yticks([])


@image_comparison(baseline_images=['rotation_anchor.png'], style='mpl20',
remove_text=True)
def test_rotation_mode_anchor():
fig, ax = plt.subplots()

ax.plot([0, 1], lw=0)
ax.axvline(.5, linewidth=.5, color='.5')
ax.axhline(.5, linewidth=.5, color='.5')

N = 4
for r in range(N):
ax.text(.5, .5, 'pP', color=f'C{r}', size=100,
rotation=r/N*360, rotation_mode='anchor',
verticalalignment='center_baseline')


@image_comparison(['axes_titles.png'])
def test_axes_titles():
# Related to issue #3327
Expand Down
Loading