Skip to content

Commit ce38485

Browse files
committed
ENH: Fewer spurious legend + after_scale warnings
1 parent f4c659d commit ce38485

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

doc/changelog.qmd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
---
22
title: Changelog
33
---
4+
## v0.13.6
5+
(not-yet-released)
6+
7+
### Enhancements
8+
9+
- Stopped spurious warnings of the form ``PlotnineWarning: Failed to apply
10+
`after_scale` modifications to the legend.`` when the `after_scale`
11+
mapping is for another aesthetic.
412

513
## v0.13.5
614
(2024-04-26)

plotnine/guides/guide_legend.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,16 +160,28 @@ def create_geoms(self):
160160
continue
161161

162162
matched = self.legend_aesthetics(l)
163+
matched_set = set(matched)
163164

164165
# This layer does not contribute to the legend
165-
if not set(matched) - exclude:
166+
if not matched_set - exclude:
166167
continue
167168

168169
data = self.key[matched].copy()
169170

170171
# Modify aesthetics
172+
173+
# When doing after_scale evaluations, we only consider those
174+
# for the aesthetics of this legend. The reduces the spurious
175+
# warnings where an evaluation of another aesthetic failed yet
176+
# it is not needed.
177+
aes_modifiers = {
178+
ae: expr
179+
for ae, expr in l.mapping._scaled.items()
180+
if ae in matched_set
181+
}
182+
171183
try:
172-
data = l.use_defaults(data)
184+
data = l.use_defaults(data, aes_modifiers=aes_modifiers)
173185
except PlotnineError:
174186
warn(
175187
"Failed to apply `after_scale` modifications "

plotnine/layer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def use_defaults(
386386
data = self.data
387387

388388
if aes_modifiers is None:
389-
aes_modifiers = self.mapping._scaled
389+
aes_modifiers = {}
390390

391391
return self.geom.use_defaults(data, aes_modifiers)
392392

tests/test_guide_internals.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import warnings
2+
3+
from plotnine import (
4+
aes,
5+
geom_point,
6+
ggplot,
7+
)
8+
from plotnine.data import mtcars
9+
10+
11+
def test_no_after_scale_warning():
12+
p = ggplot(mtcars, aes("wt", "mpg")) + geom_point()
13+
14+
with warnings.catch_warnings():
15+
warnings.simplefilter("error")
16+
p.draw_test() # type: ignore

0 commit comments

Comments
 (0)