Quickstart¶
plotnine_extra re-exports plotnine and adds geoms, stats, facets, guides, themes, plot constructors, composition helpers, and animation utilities. A single import is enough for examples that combine plotnine primitives with plotnine-extra layers.
[1]:
import numpy as np
import pandas as pd
from plotnine_extra import *
from plotnine_extra.data import ToothGrowth, flights, iris, penguins
tooth = ToothGrowth.assign(dose=ToothGrowth["dose"].astype(str))
penguin_data = penguins.dropna(subset=[
"bill_length_mm",
"bill_depth_mm",
"body_mass_g",
"species",
]).copy()
Start with plotnine syntax¶
The package keeps the plotnine grammar. Extra layers and helpers can be mixed with ggplot, aes, geoms, scales, and themes.
[2]:
penguin_data = penguins.dropna(subset=[
"bill_length_mm",
"bill_depth_mm",
"species",
]).copy()
(
ggplot(penguin_data, aes("bill_length_mm", "bill_depth_mm", color="species"))
+ geom_point(alpha=0.75)
+ geom_smooth(method="lm", se=False)
+ scale_color_tableau(k=3)
+ theme_few()
+ labs(x="Bill length (mm)", y="Bill depth (mm)", color="Species")
)
[2]:
Use constructors for common plots¶
Constructor helpers return ggplot objects, so the result can be edited with standard plotnine layers.
[3]:
(
ggboxplot(
ToothGrowth.assign(dose=ToothGrowth["dose"].astype(str)),
x="dose",
y="len",
fill="dose",
add="quasirandom",
)
+ scale_fill_few(k=3)
+ theme_pubr()
+ labs(x="Dose", y="Tooth length", fill="Dose")
)
[3]:
Combine plots after they are defined¶
Composition operators keep plots separate until layout time. This matters when plots use different scales or themes. plot_layout() and plot_annotation() control the arrangement, and are available on every supported plotnine — plotnine-extra requires plotnine 0.16.
[4]:
p1 = ggscatter(penguin_data, "bill_length_mm", "bill_depth_mm", color="species")
p2 = ggdensity(penguin_data, "body_mass_g", color="species")
combined = p1 | p2
combined = combined + plot_layout(widths=[2, 1]) + plot_annotation(
title="Penguin measurements",
caption="Palmer penguins data bundled with plotnine-extra",
)
combined
[4]:
Choose the next vignette¶
The remaining pages cover statistical annotations, geoms and positions, facets and guides, themes and palettes, constructors, and composition.