Skip to content

Commit 26728c6

Browse files
committed
fix: mapping to missing value(s) to shape
1 parent ab89e30 commit 26728c6

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

doc/changelog.qmd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ title: Changelog
2525

2626
- Fixed rendering of SVGs in jupyter notebooks.
2727

28+
- Fixed [](:class:`~plotnine.geom_legend`) to show an empty key for
29+
[](:class:`~plotnine.geom_point`) when the shape is mapped to variable with missing
30+
values.
31+
2832
## v0.15.3
2933
(2025-01-28)
3034

plotnine/guides/guide_legend.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,12 @@ def draw(self):
272272
# Drawings
273273
drawings: list[ColoredDrawingArea] = []
274274
for i in range(nbreak):
275-
da = ColoredDrawingArea(
276-
elements.key_widths[i], elements.key_heights[i], 0, 0
277-
)
275+
try:
276+
w, h = elements.key_widths[i], elements.key_heights[i]
277+
except IndexError:
278+
w, h = elements.empty_key_size
279+
280+
da = ColoredDrawingArea(w, h, 0, 0)
278281

279282
# overlay geoms
280283
for params in self._layer_parameters:
@@ -482,3 +485,14 @@ def key_heights(self) -> list[float]:
482485
if self.is_horizontal:
483486
return [max(hs)] * len(hs)
484487
return hs
488+
489+
@cached_property
490+
def empty_key_size(self) -> tuple[float, float]:
491+
"""
492+
Size of an empty key
493+
"""
494+
return (
495+
(max(self.key_widths), min(self.key_heights))
496+
if self.is_vertical
497+
else (min(self.key_widths), max(self.key_heights))
498+
)
5.39 KB
Loading

tests/test_guide_internals.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,9 @@ def alphen(series, a):
4141
)
4242

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

0 commit comments

Comments
 (0)