8000 Center of rotation for text with rotation_mode='anchor' · Issue #13044 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
timhoffm opened this issue Dec 23, 2018 · 11 comments · May be fixed by #29199
Open

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

timhoffm opened this issue Dec 23, 2018 · 11 comments · May be fixed by #29199
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! keep Items to be ignored by the “Stale” Github Action topic: text
Milestone

Comments

@timhoffm
Copy link
Member

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:
image

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?

@timhoffm timhoffm added Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! labels Jul 7, 2020
@timhoffm
Copy link
Member Author
timhoffm commented Jul 7, 2020

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.

@tacaswell
Copy link
Member

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

@dzh527
Copy link
dzh527 commented 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
Screen Shot 2020-07-14 at 11 04 37 PM

@safdarzareef
Copy link

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.

@timhoffm
Copy link
Member Author
timhoffm commented Apr 7, 2021

@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.

@anntzer anntzer changed the title Center of rotation for text with roatation_mode='anchor' Center of rotation for text with rotation_mode='anchor' Apr 20, 2021
@github-actions
Copy link

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!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label May 26, 2023
@timhoffm timhoffm added keep Items to be ignored by the “Stale” Github Action and removed status: inactive Marked by the “Stale” Github Action labels May 26, 2023
@coder-brunette
Copy link
coder-brunette commented Sep 22, 2023

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

Screen Shot 2023-09-22 at 1 55 33 PM
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).

@tacaswell
Copy link
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.
so

@coder-brunette
Copy link

@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()

a

@evelyn-lo
Copy link

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

@timhoffm
Copy link
Member Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Medium https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! keep Items to be ignored by the “Stale” Github Action topic: text
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants
0