Unexpected behavior when rotating x-axis text
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- Half a day
- Newbie friendliness
- 55/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- matplotlib, python
- Domain
- data-visualization
Research direction
Start by reading the axis text positioning and rotation code in plotnine, then check how element_text passes alignment and rotation_mode to Matplotlib. Reproduce the reported combinations from the issue with the supplied example and compare label anchors against tick positions. Done means the rotated labels can be aligned as described without shifting too far left or down.
Written by the indexing model from the issue text.
Description
First off, I'm really grateful for plotnine because for a long time, my biggest complaint with python was not having a good implementation of the grammar of graphics like R's ggplot2. In my opinion, plotnine has filled that space beautifully. That being said, I'm currently struggling with positioning tick labels on a plot I'm working on. Here's a small example to illustrate the issue I'm having:
With default parameters, everything looks as expected:
import plotnine as pn
import polars as pl
data = pl.DataFrame(
{
"x": ["aaaaa", "bbbbb", "ccccc"],
"y": [1, 1, 1]
}
)
plot = (
pn.ggplot(data, pn.aes("x", "y"))
+ pn.geom_point()
)
Output:
I can then rotate the x-axis labels just fine:
plot = (
pn.ggplot(data, pn.aes("x", "y"))
+ pn.geom_point()
+ pn.theme(
axis_text_x=pn.element_text(rotation=45)
)
)
Output:
What I'd like to achieve is shift the rotated x-axis labels to the left such that the end of the label falls directly below the corresponding tick mark. I first tried setting ha="right" but this moved the labels too far (i.e., if you trace along the center axis of the letters in each label, you get to the end of the label before you reach the x-position of the tick):
plot = (
pn.ggplot(data, pn.aes("x", "y"))
+ pn.geom_point()
+ pn.theme(
axis_text_x=pn.element_text(rotation=45, ha="right")
)
)
Output:
I figure that what I'm going for is anchoring the center-right point of the label to a point just below the tick, and then rotating around that anchor. If I set ha="right" and va="center" without rotating, it appears that the anchor point is in the correct location (i.e., directly below the tick):
plot = (
pn.ggplot(data, pn.aes("x", "y"))
+ pn.geom_point()
+ pn.theme(
axis_text_x=pn.element_text(ha="right", va="center")
)
)
Output:
But then when I rotate, it does not appear to be rotating around that anchor point. Instead the labels shift way down and also still end up positioned too far to the left:
plot = (
pn.ggplot(data, pn.aes("x", "y"))
+ pn.geom_point()
+ pn.theme(
axis_text_x=pn.element_text(rotation=45, ha="right", va="center", rotation_mode="anchor")
)
)
Output:
I've also tried lots of other combinations of values for ha, va, and rotation_mode, but can't seem to get the labels in the right position. Any help would be appreciated.
- Plotnine version: 0.15.8
- Matplotlib version: 3.11.1
- Python version: 3.11.14
- Dominant language
- Python
- Stars
- 4.8k
- Forks
- 269
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from has2k1/plotnine
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Feature request: add rotation_mode param to geom_textPossibly taken A pull request linked to this issue is open or already merged. OpenFeature
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
-
position_dodge2() leaves nested rectangles overlappingPossibly taken @MahnoorAhmed999 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
-
Enhancement
Difficulty 4/5 3-5 days Newbie friendliness 52/100
-
Discussion
Difficulty 5/5 Over a week Newbie friendliness 30/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
plone/plone.observability#114 ·
Maintainers usually reply within 1 day
-
area:docs type:docs
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
jejjohnson/gauss_flows#246 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
openvax/varcode#824 · 1 comment ·
Maintainers usually reply within 1 day
-
netbox status: needs triage type: feature
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
netbox-community/netbox#23868 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
emeryberger/CSrankings#15757 ·
Maintainers usually reply within 9 days