import pandas as pd
import numpy as np
from plotnine import ggplot, aes, geom_point
plotnine.aes
=None, y=None, kwargs={}) aes(x
Create aesthetic mappings
Parameters
x : str | array_like | scalar = None
-
x aesthetic mapping
y : str | array_like | scalar = None
-
y aesthetic mapping
**kwargs : Any = {}
-
Other aesthetic mappings
Notes
Only the x and y aesthetic mappings can be specified as positional arguments. All the rest must be keyword arguments.
The value of each mapping must be one of:
str
import pandas as pd import numpy as np = [11, 12, 13] arr = pd.DataFrame({ df "alpha": [1, 2, 3], "beta": [1, 2, 3], "gam ma": [1, 2, 3] }) # Refer to a column in a dataframe ="alpha", y="beta")) ggplot(df, aes(x
array_like
# A variable ="alpha", y=arr)) ggplot(df, aes(x # or an inplace list ="alpha", y=[4, 5, 6])) ggplot(df, aes(x
scalar
# A scalar value/variable ="alpha", y=4)) ggplot(df, aes(x # The above statement is equivalent to ="alpha", y=[4, 4, 4])) ggplot(df, aes(x
String expression
="alpha", y="2*beta")) ggplot(df, aes(x="alpha", y="np.sin(beta)")) ggplot(df, aes(x="df.index", y="beta")) ggplot(df, aes(x # If `count` is an aesthetic calculated by a stat ="alpha", y=after_stat("count"))) ggplot(df, aes(x="alpha", y=after_stat("count/np.max(count)"))) ggplot(df, aes(x
The strings in the expression can refer to;
- columns in the dataframe
- variables in the namespace
- aesthetic values (columns) calculated by the
stat
with the column names having precedence over the variables. For expressions, columns in the dataframe that are mapped to must have names that would be valid python variable names.
This is okay:
# "gam ma" is a column in the dataframe ="df.index", y="gam ma")) ggplot(df, aes(x
While this is not:
# "gam ma" is a column in the dataframe, but not # valid python variable name ="df.index", y="np.sin(gam ma)")) ggplot(df, aes(x
aes
has 2 internal methods you can use to transform variables being mapped.
factor
- This function turns the variable into a factor. It is just an alias topandas.Categorical
:="factor(cyl)")) + geom_bar() ggplot(mtcars, aes(x
reorder
- This function changes the order of first variable based on values of the second variable:= pd.DataFrame({ df "x": ["b", "d", "c", "a"], "y": [1, 2, 3, 4] }) "reorder(x, y)", "y")) + geom_col() ggplot(df, aes(
The group aesthetic
group
is a special aesthetic that the user can map to. It is used to group the plotted items. If not specified, it is automatically computed and in most cases the computed groups are sufficient. However, there may be cases were it is handy to map to it.
See Also
after_stat
-
For how to map aesthetics to variable calculated by the stat
after_scale
-
For how to alter aesthetics after the data has been mapped by the scale.
stage
-
For how to map to evaluate the mapping to aesthetics at more than one stage of the plot building pipeline.