Skip to content
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

geom_col: overlapping x-axis labels when using element_text(angle=45) #863

Closed
nick-youngblut opened this issue Aug 5, 2024 · 3 comments
Closed

Comments

@nick-youngblut
Copy link

reprex:

import plotnine as pn
from plotnine.data import mtcars

pn.ggplot(mtcars, pn.aes("name", "mpg")) \
    + pn.geom_col() \
    + pn.theme(
        figure_size=(10,3),
        axis_text_x=pn.element_text(angle=45)
    ) 

The resulting x-axis labels are not correctly positioned:

Screenshot 2024-08-05 at 10 47 12 AM

Using pn.element_text(angle=45, hjust=1) helps, but then the labels are too far to the left of each x-axis tick.

Another example from my own data:

Screenshot 2024-08-05 at 10 44 41 AM

The most relevant sections of my very long conda env:

# Name                    Version                   Build  Channel
python                    3.10.14         hd12c33a_0_cpython    conda-forge
plotnine                  0.13.6             pyhd8ed1ab_0    conda-forge
@has2k1
Copy link
Owner

has2k1 commented Aug 19, 2024

The text is rotated around the point at which it is aligned. By default that is the baseline/center. You need to change the alignment (va & ha), and also the rotation_mode which affects the order of rotation and alignment.

theme(
    axis_text_x=element_text(angle=45, va="top", ha="right", rotation_mode="anchor")
)

@nick-youngblut
Copy link
Author

Thanks @has2k1 for the info! I didn't know that plotnine differed from the ggplot2 API in this regard. I'll keep that in mind.

@has2k1
Copy link
Owner

has2k1 commented Aug 20, 2024

Reopened so that it is documented.

@has2k1 has2k1 reopened this Aug 20, 2024
@has2k1 has2k1 closed this as completed in ca92c8b Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants