diff --git a/lib/matplotlib/backends/backend_agg.py b/lib/matplotlib/backends/backend_agg.py index b435ae565ce4..f25b89e2b053 100644 --- a/lib/matplotlib/backends/backend_agg.py +++ b/lib/matplotlib/backends/backend_agg.py @@ -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): diff --git a/lib/matplotlib/tests/baseline_images/test_text/rotation_anchor.png b/lib/matplotlib/tests/baseline_images/test_text/rotation_anchor.png new file mode 100644 index 000000000000..3dad1f9a19f7 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_text/rotation_anchor.png differ diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index 407d7a96be4d..906484c8a526 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -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