Skip to content

Plotnine Warning: Failed to apply after_scale modifications to the legend #1067

@vale46

Description

@vale46

Hello -

Trying to do a plot similar to https://plotnine.org/gallery/violins-boxes-points-and-lines.html. However, in my data, I'd like use a continuous variable as a size aesthetic in geom_point. Here's a MWE with a slightly modified dataset:

import plotnine a p9
import numpy as np
import pandas as pd

np.random.seed(123)
n = 20
mu = (1, 2.3)
sigma = (1, 1.6)

before = np.random.normal(loc=mu[0], scale=sigma[0], size=n)
after = np.random.normal(loc=mu[1], scale=sigma[1], size=n)
size = np.random.uniform(10, 20, 2*n)

df = pd.DataFrame(
    {
        "value": np.hstack([before, after]),
        "when": np.repeat(["before", "after"], n),
        "point_size": np.hstack([size]),
    }
)

lsize = 0.65
fill_alpha = 0.7
m1 = p9.aes(x=p9.stage("when", after_scale="x+shift*alt_sign(x)"))  # shift outward
m2 = p9.aes(x=p9.stage("when", after_scale="x-shift*alt_sign(x)"), size='point_size') # shift inward

plot = (
    p9.ggplot(df, p9.aes("when", "value", fill="when"))
    + p9.geom_violin(m1, style="left-right", alpha=fill_alpha, size=lsize)
    + p9.geom_boxplot(width=shift, alpha=fill_alpha, size=lsize)
    + p9.geom_point(m2, color="none", alpha=fill_alpha)
    + p9.scale_fill_manual(values=["dodgerblue", "darkorange"])
    + p9.guides(fill=False)  # Turn off the fill legend
    + p9.theme_classic()
    + p9.theme(figure_size=(8, 6))
)

plot.show()

This produces the following warning message:
guide_legend.py:183: PlotnineWarning: Failed to apply after_scale modifications to the legend.

with an incomplete legend that's missing the circle in each of the legend entries (see attached image).

Image

Legend entries should not be impacted by the after_scale adjustment on this plot.

Greatly appreciate further thoughts.
Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions