has2k1 / plotnine

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

In-place string scalars in `aes` #733

Closed julian-urbano closed 6 months ago

julian-urbano commented 7 months ago

In-place use of scalars in aes is very handy, for instance to use constants or to easily differentiate groups:

import pandas as pd
from plotnine import *

# sample data
df = pd.DataFrame({'x': [1, 2, 3],
                   'y': [4, 5, 6],
                   'z': [14, 15, 16]})

# in-place mapping to numeric scalars works just fine,
# using both a constant or a list
ggplot(df, aes(x='x')) +\
    geom_point(aes(y=3)) +\
    geom_point(aes(y=[7,8,9]))

In the past (at least 0.10.1), we could also use string scalars, for example to use different colors for different datasets and have plotnine generate the mapping and scale for us:

ggplot(df, aes(x='x')) +\
    geom_point(aes(y='y', color=['Treatment'])) +\
    geom_point(aes(y='z', color=['Control']))

But I'm afraid that this doesn't work anymore (0.12.4), nor does it work when giving a full list of values, as big as the dataframe (ie. color=np.repeat('Treatment',3):

C:\ProgramData\anaconda3\Lib\site-packages\plotnine\guides\guides.py:197: PlotnineWarning: Cannot generate legend for the 'color' aesthetic. Make sure you have mapped a variable to it
has2k1 commented 6 months ago

This is a bug. Using a list with a single string should be just about equivalent to using repr string expressions. e.g.

(ggplot(df, aes(x='x'))
 +  geom_point(aes(y='y', color='"Treatment"'))
 +  geom_point(aes(y='z', color='"Control"'))
)