# plotnine.mapping.after_stat¶

plotnine.mapping.after_stat(x)[source]

Evaluate mapping after statistic has been calculated

Parameters
xstr

An expression

after_scale()

For how to alter aesthetics after the data has been mapped by the scale.

stage

For how to map to aesthetics at more than one stage of the plot building pipeline.

## Examples¶

[1]:

import pandas as pd
import numpy as np

from plotnine import *

%matplotlib inline


### after_stat¶

geom_bar uses stat_count which by default maps the y aesthetic to the count which is the number of observations at a position.

[2]:

df = pd.DataFrame({
'var1': [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5]
})

(ggplot(df, aes('var1'))
+ geom_bar()
)

[2]:

<ggplot: (97654321012345679)>


Using the after_stat function, we can instead map to the prop which is the ratio of points in the panel at a position.

[3]:

(ggplot(df, aes('var1'))
+ geom_bar(aes(y=after_stat('prop'))) # default is after_stat('count')
)

[3]:

<ggplot: (97654321012345679)>


With after_stat you can used the variables calculated by the stat in expressions. For example we use the count to calculated the prop.

[4]:

(ggplot(df, aes('var1'))
+ geom_bar(aes(y=after_stat('count / np.sum(count)')))
+ labs(y='prop')
)

[4]:

<ggplot: (97654321012345679)>


By default geom_bar uses stat_count to compute a frequency table with the x aesthetic as the key column. As a result, any mapping (other than the x aesthetic is lost) to a continuous variable is lost (if you have a classroom and you compute a frequency table of the gender, you lose any other information like height of students).

For example, below fill='var1' has no effect, but the var1 variable has not been lost it has been turned into x aesthetic.

[5]:

(ggplot(df, aes('var1'))
+ geom_bar(aes(fill='var1'))
)

[5]:

<ggplot: (97654321012345679)>


We use after_stat to map fill to the x aesthetic after it has been computed.

[6]:

(ggplot(df, aes('var1'))
+ geom_bar(aes(fill=after_stat('x')))
+ labs(fill='var1')
)

[6]:

<ggplot: (97654321012345679)>