Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/changelog.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ title: Changelog

### Bug Fixes

- In a non-linear coordinate system (e.g. [](:class:`~plotnine.coord_trans`)),
the closing edge of a polygon is now curved along with the rest of its
boundary instead of being drawn as a straight chord.

- The space between facet panels now accounts for the margins of the axis
text, so with free scales large margins no longer push the tick labels
into the neighbouring panel.
Expand Down
13 changes: 13 additions & 0 deletions plotnine/geoms/geom_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ def draw_group(
):
from matplotlib.collections import PolyCollection

# A polygon is a closed ring, but the vertices only trace it open.
# In a non-linear coord the closing edge must be munched like any
# other, or it stays a straight chord across a curved boundary (e.g.
# the inner arc of a polar bar). Repeat each group's first vertex so
# munch subdivides that edge too; a linear coord closes it straight,
# which is already correct, so leave it untouched there.
if not coord.is_linear:
indices = data.groupby("group", sort=False).indices
order = np.concatenate(
[np.append(idx, idx[0]) for idx in indices.values()]
)
data = data.iloc[order].reset_index(drop=True)

data = coord.transform(data, panel_params, munch=True)
data["linewidth"] = data["size"] * SIZE_FACTOR

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions tests/test_coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
geom_bar,
geom_line,
geom_point,
geom_polygon,
ggplot,
xlim,
)
Expand Down Expand Up @@ -74,6 +75,21 @@ def test_coord_trans_backtransforms():
assert p == "coord_trans_backtransform"


def test_coord_trans_munches_polygon_closing_edge():
# A polygon is a closed ring, but its vertices only trace it open; the
# edge from the last vertex back to the first must be munched like any
# other. The triangle's two explicit edges are axis-aligned (straight
# under log), so only the closing edge curves — if it is left as a
# straight chord this baseline shows a triangle instead of the arc.
tri = pd.DataFrame({"x": [1.0, 1.0, 100.0], "y": [100.0, 1.0, 1.0]})
p = (
ggplot(tri, aes("x", "y"))
+ geom_polygon(fill="none", color="black", size=1)
+ coord_trans(x="log10", y="log10")
)
assert p == "coord_trans_munches_polygon_closing_edge"


def test_datetime_coord_limits():
n = 6

Expand Down
Loading