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 @@ -25,6 +25,10 @@ title: Changelog

- Fixed rendering of SVGs in jupyter notebooks.

- Fixed [](:class:`~plotnine.geom_legend`) to show an empty key for
[](:class:`~plotnine.geom_point`) when the shape is mapped to variable with missing
values.

## v0.15.3
(2025-01-28)

Expand Down
20 changes: 17 additions & 3 deletions plotnine/guides/guide_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,12 @@ def draw(self):
# Drawings
drawings: list[ColoredDrawingArea] = []
for i in range(nbreak):
da = ColoredDrawingArea(
elements.key_widths[i], elements.key_heights[i], 0, 0
)
try:
w, h = elements.key_widths[i], elements.key_heights[i]
except IndexError:
w, h = elements.empty_key_size

da = ColoredDrawingArea(w, h, 0, 0)

# overlay geoms
for params in self._layer_parameters:
Expand Down Expand Up @@ -482,3 +485,14 @@ def key_heights(self) -> list[float]:
if self.is_horizontal:
return [max(hs)] * len(hs)
return hs

@cached_property
def empty_key_size(self) -> tuple[float, float]:
"""
Size of an empty key
"""
return (
(max(self.key_widths), min(self.key_heights))
if self.is_vertical
else (min(self.key_widths), max(self.key_heights))
)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions tests/test_guide_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ def alphen(series, a):
)

assert p == "guide_legend_after_scale"


def test_guide_legend_missing_value_for_shapes():
data = pd.DataFrame({"a": [1, 2, 3], "b": ["a", None, "z"]})
p = ggplot(data, aes("a", "b")) + geom_point(aes(shape="b"), na_rm=True)
assert p == "guide_legend_missing_value_for_shapes"