plotnine icon indicating copy to clipboard operation
plotnine copied to clipboard

Bug: Arrows are distorted/squashed when using `coord_flip`

Open AlFontal opened this issue 2 weeks ago • 0 comments

When using coord_flip(), arrowheads drawn by geom_segment (or indirectly, geom_path) appear completely distorted or squashed. The arrow shape calculation seems to be happening in data coordinates that do not account for the flipped axes.

Reproducible example

import pandas as pd
import plotnine as p9
df = pd.DataFrame({
    "sample": ["A", "B", "C", "D", "E"],
    "value":  [0.1, 0.3, 0.6, 0.4, 0.8],
})
(p9.ggplot(df, p9.aes(x="sample", y="value"))
    + p9.geom_segment(
        p9.aes(xend="sample", y=0, yend="value"),
        arrow=p9.arrow(angle=30, length=0.2, ends="last", type="open"),
    )
    + p9.labs(x="", y="Value", title="Standard")
)
Image

However, if we use coord_flip():


(p9.ggplot(df, p9.aes(x="sample", y="value"))
    + p9.geom_segment(
        p9.aes(xend="sample", y=0, yend="value"),
        arrow=p9.arrow(angle=30, length=0.2, ends="last", type="open"),
    )
    + p9.coord_flip()
    + p9.labs(x="", y="Value", title="Coord Flip")
)
Image

The expected behaviour would be the one we would have if we didn't use coord_flip() but manually changed the x and y variables, so that:

Image

I suspect that geom_path(which handles arrow drawing) is calculating the arrow aspect ratio corrections using data ranges that don't correctly account for the axis swap introduced by coord_flip.

It seems to be using the original x-variable range for width even when coord_flip moves it to the vertical axis.

I will open a PR with a fix if it turns out to be this mismatch and it's easy to solve.

AlFontal avatar Dec 05 '25 16:12 AlFontal