import pandas as pd
import plotnine as p9Plotting with the grammar of graphics
A classic example
Let’s get things set up:
and use the Palmer Penguins dataset
from plotnine.data import penguinsthe data
penguinsthe data, v2
pd.crosstab(penguins.species, penguins.island)Let’s make some plots!
(
p9.ggplot(penguins,
p9.aes(x='flipper_length_mm', y='body_mass_g', color='species')
) + p9.geom_point()
)What’s going on there?
plotnine is basically a port of ggplot2 for python.
Have a look at the cheatsheet:
ggplot(
data,
aes(...)
) + geom_function()
or, with all the bells and whistles:
ggplot(data=data_frame)
+ geom_function(
mapping=aes(**mappings)
stat=stat,
position=position,
) + coord_function()
+ facet_function()
+ scale_function()
+ theme_function()
+ theme(**settings)
Let’s try it out!
YTS data
Here’s some summarized data from the YTS:
yts = pd.read_csv("data/yts_summarized.csv")
yts