Skip to content

Commit 470c1d2

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

File tree

5 files changed

+44
-18
lines changed

5 files changed

+44
-18
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/ggplot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def _build(self):
400400
layout.setup_panel_params(self.coordinates)
401401

402402
# fill in the defaults
403-
layers.use_defaults()
403+
layers.use_defaults_after_scale()
404404

405405
# Allow stats to modify the layer data
406406
layers.finish_statistics()

plotnine/guides/guide_legend.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,23 +160,35 @@ 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)
173185
except PlotnineError:
174186
warn(
175187
"Failed to apply `after_scale` modifications "
176188
"to the legend.",
177189
PlotnineWarning,
178190
)
179-
data = l.use_defaults(data, aes_modifiers={})
191+
data = l.use_defaults(data, {})
180192

181193
# override.aes in guide_legend manually changes the geom
182194
for ae in set(self.override_aes) & set(data.columns):

plotnine/layer.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,8 @@ def draw(self, layout: Layout, coord: coord):
368368

369369
def use_defaults(
370370
self,
371-
data: pd.DataFrame | None = None,
372-
aes_modifiers: dict[str, Any] | None = None,
371+
data: pd.DataFrame,
372+
aes_modifiers: dict[str, Any],
373373
) -> pd.DataFrame:
374374
"""
375375
Prepare/modify data for plotting
@@ -382,12 +382,6 @@ def use_defaults(
382382
Expression to evaluate and replace aesthetics in
383383
the data.
384384
"""
385-
if data is None:
386-
data = self.data
387-
388-
if aes_modifiers is None:
389-
aes_modifiers = self.mapping._scaled
390-
391385
return self.geom.use_defaults(data, aes_modifiers)
392386

393387
def finish_statistics(self):
@@ -474,13 +468,9 @@ def compute_position(self, layout: Layout):
474468
for l in self:
475469
l.compute_position(layout)
476470

477-
def use_defaults(
478-
self,
479-
data: pd.DataFrame | None = None,
480-
aes_modifiers: dict[str, Any] | None = None,
481-
):
471+
def use_defaults_after_scale(self):
482472
for l in self:
483-
l.use_defaults(data, aes_modifiers)
473+
l.use_defaults(l.data, l.mapping._scaled)
484474

485475
def transform(self, scales: Scales):
486476
for l in self:

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)