Plotting with the grammar of graphics

Peter Ralph

2026-01-12

A classic example

Let’s get things set up:

import pandas as pd
import plotnine as p9

and use the Palmer Penguins dataset

from plotnine.data import penguins

the data

penguins

the 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