has2k1 / plotnine

A Grammar of Graphics for Python
https://plotnine.org
MIT License
3.89k stars 209 forks source link

`geom_smooth` doesn't apply `alpha` #770

Closed david-cortes closed 2 months ago

david-cortes commented 2 months ago

Passing argument alpha to geom_smooth doesn't end up applying transparency.

Example:

import numpy as np, pandas as pd, plotnine as p9
df = pd.DataFrame({
    "x": np.arange(100),
    "y" : np.arange(100) * np.random.random(size=100)
})
print(
    p9.ggplot(df, p9.aes(x="x", y="y"))
    + p9.geom_point(alpha=0.1)
    + p9.geom_smooth(alpha=0.1)
)

image

has2k1 commented 2 months ago

For geom_smooth the alpha aesthetic only applies to the confidence interval because commonly, you only want to see through that area and not the line.

You can set the colour of the line to rgba hex value e.g. color="#00000077" to get a transparent line.

Or, if you are mapping the color to a variable, you use staged evaluation to modify the color from rgb to rgba i.e.

aes(color=stage("col1", after_scale="make_transparent(color)"))

where

def make_transparent(rgb_seq):
    return [f"{c}77" for c in rgb_seq]