Skip to content

Commit

Permalink
ENH: Fewer spurious legend + after_scale warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
has2k1 committed May 6, 2024
1 parent f4c659d commit ce38485
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
8 changes: 8 additions & 0 deletions doc/changelog.qmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
---
title: Changelog
---
## v0.13.6
(not-yet-released)

### Enhancements

- Stopped spurious warnings of the form ``PlotnineWarning: Failed to apply
`after_scale` modifications to the legend.`` when the `after_scale`
mapping is for another aesthetic.

## v0.13.5
(2024-04-26)
Expand Down
16 changes: 14 additions & 2 deletions plotnine/guides/guide_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,28 @@ def create_geoms(self):
continue

matched = self.legend_aesthetics(l)
matched_set = set(matched)

# This layer does not contribute to the legend
if not set(matched) - exclude:
if not matched_set - exclude:
continue

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

# Modify aesthetics

# When doing after_scale evaluations, we only consider those
# for the aesthetics of this legend. The reduces the spurious
# warnings where an evaluation of another aesthetic failed yet
# it is not needed.
aes_modifiers = {
ae: expr
for ae, expr in l.mapping._scaled.items()
if ae in matched_set
}

try:
data = l.use_defaults(data)
data = l.use_defaults(data, aes_modifiers=aes_modifiers)
except PlotnineError:
warn(
"Failed to apply `after_scale` modifications "
Expand Down
2 changes: 1 addition & 1 deletion plotnine/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def use_defaults(
data = self.data

if aes_modifiers is None:
aes_modifiers = self.mapping._scaled
aes_modifiers = {}

return self.geom.use_defaults(data, aes_modifiers)

Expand Down
16 changes: 16 additions & 0 deletions tests/test_guide_internals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import warnings

from plotnine import (
aes,
geom_point,
ggplot,
)
from plotnine.data import mtcars


def test_no_after_scale_warning():
p = ggplot(mtcars, aes("wt", "mpg")) + geom_point()

with warnings.catch_warnings():
warnings.simplefilter("error")
p.draw_test() # type: ignore

0 comments on commit ce38485

Please sign in to comment.