GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix center of rotation with rotation_mode='anchor' by WPurre · Pull Request #29199 · matplotlib/matplotlib

Conversation

@WPurre

@WPurre WPurre commented Nov 27, 2024

Copy link
Copy Markdown

PR summary

This will close #13044.

I see that there is a pull request already #20057, however that has not been active for a long time and has since been closed.
I believe that they correctly identified the cause of the bug in that pull request but that their implementations wasn't quite right.
As such this pull request picks up where they left off.

I will be using the following code from the issue as a way of showing the result of the code:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(2, 2)
for idx, ax in enumerate(axes.flatten()):
      ax.axvline(.5, linewidth=.5, color='.5')
      ax.axhline(.5, linewidth=.5, color='.5')
      ax.text(
          .5, .5, 'pP', size=50,
          bbox=dict(facecolor='None', edgecolor='red', pad=0),
          rotation=idx*90,
          va='center_baseline',
          rotation_mode='anchor'
      )

This code produces the following plot:
image

After the changes the plot changes to:
result

Showing that the text now properly rotates around the point (0.5, 0.5) instead of the leftmost side of the text.
We can also see that the text more properly aligns with the bounding box.

As was diagnosed in #20057 the issue is caused by the offset of the text not being rotated with the rest of the text, causing the text to rotate around itself rather than the point it should be rotating around.

This understandably causes some tests to fail since rotated text will look slightly different.
Here is an example of what the difference looks like for a failed test, along with the produced image:
difference_constrained_layout2
image

As far as I can tell all of the failed tests are of this nature, rotated text being moved slightly due to the new logic.
The only test that differs is tight_layout1 where the images look like this:

difference_tight_layout1
image

I believe that the difference in the line is caused by the available space being changed by moving the text but would love feedback on this.

PR checklist

  • "closes #0000" is in the body of the PR description to link the related issue
  • new and changed code is tested
  • Plotting related features are demonstrated in an example
  • [N/A] New Features and API Changes are noted with a directive and release note
  • [N/A] Documentation complies with general and docstring guidelines

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@github-actions github-actions Bot added the backend: agg label Nov 27, 2024
github-actions[bot]
github-actions Bot reviewed Nov 27, 2024
View reviewed changes

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ksunden

ksunden commented Dec 4, 2024

Copy link
Copy Markdown
Member

Empirically, I think I agree that the changed behavior is better (at least with the main example provided here, and don't have any reason to suspect it is special).

However, the main hangup with the previous PR appears to be a request for an explanation of why the offset math works out this way in the comments (phrased as a request for a "brief proof"), which I do not think this PR provides at this time.

If I read the code correctly the primary idea is that you are adjusting the anchor point by the applied rotation. The comment that exists "Standard 2D rotation transformation" does not in my opinion adequately explain it's connection to the anchor point. The change in sign in the rounding line is also mildly confusing to me (not saying it is wrong, just not sure why it changed)

Finally, yes, before this can be merged, the tests will have to be updated so they are passing. I don't love the quantity (or magnitude) of changes, as we do try to have stable outputs. However, correctness is certainly a good reason to change them.

I'm not sure why the one test changes the line at all... if it was positioning/spacing of the axes I'd have expected the axis frame to have differences in the diff output. Further confused why it has a gradient of magnitude of the change.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@WPurre
WPurre force-pushed the fixing-rotation-bug branch 2 times, most recently from f26ed0e to 5739086 Compare December 8, 2024 14:51
@WPurre

WPurre commented Dec 8, 2024

Copy link
Copy Markdown
Author

Thank you for the comment.

I've updated the code to hopefully adequately explain the logic behind the changes.
Regarding the failing tests, should I include the updated images in this pull request or is there some other procedure for how to update them (assuming that this is the correct solution and that they should be changed)?

What's the best thing to do about the tight_layout test? I don't see why the image is changed but at the same time I see no issues with other plots that I think would be present if my changes did something weird with the plotting logic.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@QuLogic

QuLogic commented Dec 12, 2024

Copy link
Copy Markdown
Member

I'm not sure why the one test changes the line at all... if it was positioning/spacing of the axes I'd have expected the axis frame to have differences in the diff output. Further confused why it has a gradient of magnitude of the change.

That's a tight layout test; when the text moves a little bit, so does everything else. It's fine so long as the text movement is expected.

Regarding the failing tests, should I include the updated images in this pull request or is there some other procedure for how to update them (assuming that this is the correct solution and that they should be changed)?

Yes, you should update the test images in the same PR. You can use tools/triage_tests.py to check them.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@WPurre
WPurre force-pushed the fixing-rotation-bug branch from 5739086 to 14dc50d Compare December 12, 2024 13:01
@github-actions github-actions Bot added the topic: mplot3d label Dec 12, 2024
@WPurre
WPurre force-pushed the fixing-rotation-bug branch 2 times, most recently from 8269aa0 to 6009467 Compare December 12, 2024 16:21
@WPurre

WPurre commented Dec 12, 2024

Copy link
Copy Markdown
Author

Yes, you should update the test images in the same PR. You can use tools/triage_tests.py to check them.

I've now updated the images to the new ones. Thank you for the information.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

scottshambaugh
scottshambaugh approved these changes Jan 30, 2025
View reviewed changes

@scottshambaugh scottshambaugh left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks great, thank you for the fix!

  • The math looks good, and the comments explain what's happening
  • Your plots to show the bounding box change make sense and show the problem & solution well
  • I think the difference is most noticeable in the test contour labels plot, where the new labels looks noticeably better centered and spaced.

I would recommend this be released with v3.11.0 rather than v3.10.2, since it's a minor visual bug that may impact a large number of downstream image tests.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@scottshambaugh scottshambaugh added this to the v3.11.0 milestone Jan 30, 2025
@tacaswell

tacaswell commented Jan 31, 2025

Copy link
Copy Markdown
Member

Can you add a new image test which is the reproducer from this issue? Although any future changes to this will clearly cause a bunch of failures, having one very clear test case is still very valuable.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the status: needs rebase label Apr 10, 2025
@QuLogic
QuLogic force-pushed the fixing-rotation-bug branch from 6009467 to a143ac8 Compare June 7, 2025 07:46
@github-actions github-actions Bot removed the status: needs rebase label Jun 7, 2025
@QuLogic

QuLogic commented Jun 7, 2025

Copy link
Copy Markdown
Member

I rebased and pushed the test from the original issue. Several tight layout/constrained layout test images were dropped here because we switched them all to placeholders.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@QuLogic
QuLogic force-pushed the fixing-rotation-bug branch from a143ac8 to cf34115 Compare June 7, 2025 08:14
@QuLogic
QuLogic changed the base branch from main to text-overhaul June 7, 2025 08:40
@QuLogic QuLogic added this to Font and text overhaul Jun 7, 2025
@github-project-automation github-project-automation Bot moved this to Waiting for other PR in Font and text overhaul Jun 7, 2025
@QuLogic QuLogic moved this from Waiting for other PR to Ready for Review in Font and text overhaul Jun 7, 2025
Wiliam @QuLogic
Fix center of rotation with rotation_mode='anchor'
c44db77
@QuLogic
QuLogic force-pushed the fixing-rotation-bug branch from cf34115 to c44db77 Compare June 10, 2025 20:50
@github-actions github-actions Bot removed the topic: mplot3d label Jun 10, 2025
@QuLogic

QuLogic commented Jun 10, 2025

Copy link
Copy Markdown
Member

While I did do the rebase and add the test, it was really by @timhoffm from the issue, so I'm going to count my adding it as an approval of its contents.

As I am merging this to the text-overhaul branch, I stripped the image changes here, and will not wait for the CI, as it's just going to fail.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@QuLogic
QuLogic merged commit f1cdc19 into matplotlib:text-overhaul Jun 10, 2025
@github-project-automation github-project-automation Bot moved this from Ready for Review to Done in Font and text overhaul Jun 10, 2025
@github-project-automation github-project-automation Bot moved this from Needs review to Waiting for author in First Time Contributors Jun 10, 2025
@QuLogic QuLogic moved this from Waiting for author to Merged in First Time Contributors Jun 10, 2025
@QuLogic

QuLogic commented Jun 10, 2025

Copy link
Copy Markdown
Member

Thanks @WPurre! Congratulations on your first PR to Matplotlib 🎉 We hope to hear from you again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@QuLogic QuLogic mentioned this pull request Jun 10, 2025
Drop the FT2Font intermediate buffer. #30059
Merged
5 tasks
@QuLogic QuLogic mentioned this pull request Jun 30, 2025
[Bug]: Text rotation leads to characters being misplaced within their bounding boxes. Attempted solution provided. #23021
Closed
@QuLogic QuLogic linked an issue Jun 30, 2025 that may be closed by this pull request
rotated text does not align #14239
Closed
@QuLogic QuLogic mentioned this pull request Jun 30, 2025
rotated text does not align #14239
Closed
@QuLogic QuLogic linked an issue Jun 30, 2025 that may be closed by this pull request
[Bug]: Text rotation leads to characters being misplaced within their bounding boxes. Attempted solution provided. #23021
Closed
wavebyrd pushed a commit to wavebyrd/matplotlib that referenced this pull request Mar 13, 2026
@QuLogic
Merge pull request matplotlib#29199 from WPurre/fixing-rotation-bug
21aa4b3
Fix center of rotation with rotation_mode='anchor'
@QuLogic QuLogic mentioned this pull request Mar 19, 2026
Center of rotation for text with rotation_mode='anchor' #13044
Closed
QuLogic added a commit to QuLogic/matplotlib that referenced this pull request Apr 10, 2026
@QuLogic
Update test images for font/text overhaul
12d2a4d
This includes images changes for the following pull requests / commits:

* [Fix center of rotation with
  rotation_mode='anchor'](matplotlib#29199)
  (c44db77)
* [Remove ttconv backwards-compatibility
  code](matplotlib#30145)
  (8caff88)
* [Remove kerning_factor from
  tests](matplotlib#29816)
  (7b4d725)
* [Set text hinting to
  defaults](matplotlib#29816)
  (8255ae2)
* [Update FreeType to
  2.13.3](matplotlib#29816)
  (89c054d)
* [Implement text shaping with
  libraqm](matplotlib#30000)
  (b0ded3a,
  9813523)
* [Add language parameter to Text
  objects](matplotlib#29794)
  (7ce8eae)
* [Fix auto-sized glyphs with BaKoMa
  fonts](matplotlib#29936)
  (3ba2c13)
* [pdf: Improve text with characters outside embedded font
  limits](matplotlib#30512)
  (b70fb88,
  6cedcf7)
* [Prepare `CharacterTracker` for advanced font
  features](matplotlib#30608)
  (8274e17,
  70dc388,
  df670cf,
  ed5e074)
* [Add font feature API to
  Text](matplotlib#29695)
  (972a688)
* [Fix spacing in r"$\max
  f$"](matplotlib#30715)
  (4a99a83)
* [Implement libraqm for vector
  outputs](matplotlib#30607)
  (bd17cd4)
* [Drop the FT2Font intermediate
  buffer](matplotlib#30059)
  (9d7d7b4)
* [Rasterize dvi files without
  dvipng](matplotlib#30039)
  (7627118)
* [Update bundled FreeType and HarfBuzz
  libraries](matplotlib#30938)
  (a161658,
  9619bcc)
* [Fix positioning of wide mathtext
  accents](matplotlib#31069)
  (c2fa7ba)
* [Refactor RendererAgg.draw_{mathtext,text,tex} to use same base
  algorithm](matplotlib#31085)
  (931bcf3)
* [Implement TeX's fraction and script
  alignment](matplotlib#31046)
  (94ff452,
  4bfa0f9,
  1cd8510)
* [Fix confusion between text height and ascent in metrics
  calculations](matplotlib#31107)
  (60f2310)
* [mathtext: Fetch quad width & axis height from font
  metrics](matplotlib#31110)
  (692df3f,
  383028b)
* [mathtext: add mathnormal and distinguish between normal and italic
  family](matplotlib#31121)
  (a6913f3)
* [ENH: Ignore empty text for
  tightbbox](matplotlib#31285)
  (d772043)
* [Drop axis_artist tickdir image compat, due to text-overhaul
  merge](matplotlib#31281)
  (2057583)
* [text: Use font metrics to determine line
  heights](matplotlib#31291)
  (3ab6a27,
  d961462,
  97f4943)
* [ps/pdf: Override font height metrics to support AFM
  files](matplotlib#31371)
  (e0913d4)
* [TST: Cleanup back-compat code in tests touched by text
  overhaul](matplotlib#31295)
  (7c33379)
* [TST: Set tests touched by text overhaul to mpl20
  style](matplotlib#31300)
  (41c4d8d)
@QuLogic QuLogic mentioned this pull request Apr 10, 2026
Font and text overhaul #30161
Merged
4 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@github-actions github-actions[bot] github-actions[bot] left review comments
@scottshambaugh scottshambaugh scottshambaugh approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

backend: agg

Projects

First Time Contributors
Status: Merged
Font and text overhaul
Status: Done

Milestone

v3.11.0

Development

Successfully merging this pull request may close these issues.

[Bug]: Text rotation leads to characters being misplaced within their bounding boxes. Attempted solution provided. rotated text does not align Center of rotation for text with rotation_mode='anchor'

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@WPurre @ksunden @QuLogic @tacaswell @scottshambaugh
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.