Skip to content

Conversation

@AlFontal
Copy link

@AlFontal AlFontal commented Dec 5, 2025

Description

This PR fixes a bug where arrowheads in geom_path (and derivatives like geom_segment) were distorted when using coord_flip().

The issue was caused by geom_path using coord.range(panel_params) to calculate the data range for aspect ratio corrections. coord_flip.range() returns the ranges of the original data variables (where x is the mapped variable, which becomes vertical). However, geom_path expects the ranges to correspond to the physical visual axes (horizontal and vertical width).

The fix is to use panel_params.x.range and panel_params.y.range directly, as plotnine ensures panel_params.x always corresponds to the horizontal visual axis.

Changes

  • Modified plotnine/geoms/geom_path.py: updated get_paths to access panel_params.x.range and panel_params.y.range directly.

Before:

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.coord_flip()
)
image

Now:

(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"),
    )
    + p9.coord_flip()
)
image

Related issue

Closes #1014.

@AlFontal
Copy link
Author

AlFontal commented Dec 5, 2025

I ran the test suite locally and it seems to pass. Wondering whether it might make sense to create an actual test_coord_flip test that compares the png's generated by manually flipping and coord flipping, but I worry that the implementation doesn't 100% work pixel-perfect as of right now...

@AlFontal AlFontal marked this pull request as ready for review December 5, 2025 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Arrows are distorted/squashed when using coord_flip

1 participant