FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Center of rotation for text with rotation_mode='anchor' · Issue #13044 · matplotlib/matplotlib · GitHub

Repository navigation

Center of rotation for text with rotation_mode='anchor' #13044

Closed

Description

Bug report

After fixing an obvious vertical offset bug in #13029 the rotation

import matplotlib.pyplot as plt
plt.plot([0, 1], lw=0)
plt.axvline(.5, linewidth=.5, color='.5')
plt.axhline(.5, linewidth=.5, color='.5')

N = 4
for r in range(N):
    plt.text(.5, .5, 'pP', color=plt.get_cmap('jet')(r/N), size=100, rotation=r/N*360, va='center_baseline', rotation_mode='anchor')

looks like this:

I'm not sure if this is now 100% as desired:

  • The vertical position of the rotation point is not exactly at y=0.5 (slightly shifted down).
  • The horizontal position of the rotation point is not at x=0.5 but at the left edge of the first letter.

Is this intended or are there still other bugs in the position calculation?

Activity

  1. added
    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
    🌱 Good first issueOpen a pull request against these issues if there are no active ones!
    on Jul 7, 2020
  2. timhoffm commented on Jul 7, 2020

    MemberAuthor

    Labelled as

    • "Good first issue": You just have to follow the rotation code.
    • "Difficulty medium": Understand the mathematics for the rotation center and adjust it.
  3. tacaswell commented on Jul 12, 2020

    Member

    We should also check that this is consistent between the backends (ie, png / svg / pdf all have the same problem).

  4. dzh527 commented on Jul 15, 2020

    @timhoffm Looks like the problem is not with the rotation center, but with the bounding box and the text:

    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'
          )

    and this will produce

  5. safdarzareef commented on Apr 6, 2021

    Hi everyone, I am Zareef, and I am very new to contributing to open source projects. I was wondering if me and my friend could contribute to this issue for a class at my university.

  6. timhoffm commented on Apr 7, 2021

    MemberAuthor

    @safdarzareef You are very welcome to contribute if you think you're basically able to resolve the issue. While we're available for targeted questions, we don't have the capacity for close mentoring (We had some bad experiences in the past from university class contributors that essentially needed as step-by-step guidance how to approach and resolve the issue).

    Also there's already a proposed patch #19114. So first step would be to check that and determine if this is a good approach and only needs some additions.

  7. changed the title [-]Center of rotation for text with roatation_mode='anchor'[/-] [+]Center of rotation for text with rotation_mode='anchor'[/+] on Apr 20, 2021
  8. github-actions commented on May 26, 2023

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  9. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on May 26, 2023
  10. coder-brunette commented on Sep 22, 2023

    Is this issue still open? If yes, does this make sense or look little better? @timhoffm

    import matplotlib.pyplot as plt
    
    plt.plot([0, 1], lw=0)
    plt.axvline(.5, linewidth=.5, color='.5')
    plt.axhline(.5, linewidth=.5, color='.5')
    
    N = 4
    for r in range(N):
        text = plt.text(.5, .5, 'pP', color=plt.get_cmap('jet')(r/N), size=100, rotation=r/N*360, va='center_baseline', rotation_mode='anchor')
        text.set_rotation(r/N*360) 
        text.set_position((.5, .5)) 
    
    plt.show()

    seems like it handles this issue -> The vertical position of the rotation point is not exactly at y=0.5 (slightly shifted down).

  11. tacaswell commented on Sep 22, 2023

    Member

    Update: This still seems to be a problem and the issue appears to be related to the bounding box of the rotated text not being correct.

  12. coder-brunette commented on Sep 23, 2023

    @tacaswell tried making this change!

    import matplotlib.pyplot as plt
    import numpy as np
    
    def add_rotated_text(ax, text, x, y, angle, size=50, color='black', bbox_pad=0):
        rotation_rad = np.deg2rad(angle)
        x_adjusted = x + 0.03 * np.cos(rotation_rad)
        y_adjusted = y + 0.03 * np.sin(rotation_rad)
        
        text_obj = ax.text(
            x_adjusted, y_adjusted, text, size=size,
            rotation=angle,
            va='center',
            rotation_mode='anchor',
            color=color
        )
        
        # Set a fixed padding value for the bounding box
        text_obj.set_bbox(dict(facecolor='None', edgecolor='red', pad=bbox_pad))
    
    # Create a 2x2 grid of subplots
    fig, axes = plt.subplots(2, 2)
    
    # Loop through subplots and add rotated text
    for idx, ax in enumerate(axes.flatten()):
        ax.axvline(.5, linewidth=.5, color='.5')
        ax.axhline(.5, linewidth=.5, color='.5')
        add_rotated_text(ax, 'pP', .5, .5, idx*90, bbox_pad=0)  # Specify bbox_pad here
    
    # Add labels and title
    fig.suptitle('Rotated Text Example')
    axes[1, 0].set_xlabel('X-axis')
    axes[1, 1].set_xlabel('X-axis')
    axes[0, 0].set_ylabel('Y-axis')
    axes[1, 0].set_ylabel('Y-axis')
    
    plt.show()
    

  13. added this to the v3.11.0 milestone on Jan 30, 2025
  14. evelyn-lo commented on Apr 20, 2025

    @timhoffm Hello! I'm Evelyn, and I'm interested in helping resolve this issue. Can me and my friends work on this issue?

  15. timhoffm commented on Apr 21, 2025

    MemberAuthor

    @evelyn-lo sure. This is a classic debug topic. You need to find out why the behavior is like it is. It's likely somewhere in the numerics of the calculation.

  16. sahil-11116 commented on Mar 5, 2026

    Hi, I’m a new contributor interested in working on this issue. If it’s still available, I’d like to investigate it and try implementing a fix.

  17. QuLogic commented on Mar 19, 2026

    Member

    #29199 will fix this; there is no need to work on this now.

  18. QuLogic commented on Apr 10, 2026

    Member

    The text-overhaul branch is now merged to main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issueskeepItems to be ignored by the “Stale” Github Actiontopic: text🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


    Back | FazBrowse Home | New Git URL