Skip to content

Bug: Arrows are distorted/squashed when using coord_flip #1014

@AlFontal

Description

@AlFontal

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions