Skip to content

Commit 14dc50d

Browse files
author
Wiliam
committed
Implement the changes to the text rotation
1 parent d45f971 commit 14dc50d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+14
-4
lines changed

lib/matplotlib/backends/backend_agg.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,20 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
197197
xo, yo = font.get_bitmap_offset()
198198
xo /= 64.0
199199
yo /= 64.0
200-
xd = d * sin(radians(angle))
201-
yd = d * cos(radians(angle))
202-
x = round(x + xo + xd)
203-
y = round(y + yo + yd)
200+
201+
rad = radians(angle)
202+
xd = d * sin(rad)
203+
yd = d * cos(rad)
204+
# Rotating the offset vector ensures text rotates around the anchor point.
205+
# Without this, rotated text offsets incorrectly, causing a horizontal shift.
206+
# Applying the 2D rotation matrix.
207+
rotated_xo = xo * cos(rad) - yo * sin(rad)
208+
rotated_yo = xo * sin(rad) + yo * cos(rad)
209+
# Subtract rotated_yo to account for the inverted y-axis in computer graphics,
210+
# compared to the mathematical convention.
211+
x = round(x + rotated_xo + xd)
212+
y = round(y - rotated_yo + yd)
213+
204214
self._renderer.draw_text_image(font, x, y + 1, angle, gc)
205215

206216
def get_text_width_height_descent(self, s, prop, ismath):

0 commit comments

Comments
 (0)