The seaborn objects interface
Last updated
seaborn has a second way to make charts, built from parts instead of chosen from a list. You start with the data and what maps to what, then add marks, stats that summarise and moves that shift, one layer at a time. The idea is older than seaborn: it's Leland Wilkinson's grammar of graphics. In this lesson you'll build a chart piece by piece, and see how the same few parts make many kinds of chart.
By the end of this lesson you can
- Build charts with so.Plot by adding marks, stats and moves
- Name the parts of the grammar of graphics in a chart
- Add facets, scales and labels to an objects chart
A grammar for charts
scatterplot, barplot, histplot: the functions so far are a menu. Each draws one kind of chart, and when you want something that isn't on the menu, you're stuck.
In 1999 the statistician Leland Wilkinson described charts differently, as a grammar: any chart is put together from a few kinds of part.
| Part | What it is | In the chart below |
|---|---|---|
| Data | the table | the penguins |
| Mapping | which column goes to which aesthetic | species across, body mass up |
| Mark | what's drawn for each row or summary | dots, a dash |
| Stat | a calculation on the data before drawing | the mean of each species |
| Move | a shift so marks don't hide each other | jitter, dodge |
| Scale | how a value becomes a position, colour or size | which colour each species gets |
| Facet | splitting into panels | (none yet: step 3 has one) |
The same idea is behind R's ggplot2, Altair (7.9) and seaborn.objects. Step through a chart being built:
so.Plot(penguins, x="species", y="body_mass_g") names the table and says which column goes where. That’s enough for axes, but nothing is drawn yet.Build it one layer at a time
Now in code. Run the cells.
so.Plot(penguins, x="species", y="body_mass_g")is the data and the mapping. Shown on its own, it draws empty axes, already labelled..add(so.Dot())adds a layer with a mark. Each.addreturns a newPlot, so calls chain.- The third cell adds two layers. The first has a move,
so.Jitter(0.4), which spreads the dots sideways. The second has a stat,so.Agg(), which turns each species' rows into one value (the mean, by default), drawn with aso.Dashmark. The stat is a separate part from the mark:so.Agg("median")would give a median dash, andso.Dot(), so.Agg()a dot at each mean.
The whole chart is wrapped in brackets so each part can go on its own line, a common style for long chains. Like any last expression in a cell, a Plot is drawn when the cell shows it.
Same parts, new charts
Run all: the first three cells are step 2's again, and the last three are new charts from the same few parts.
- Counted bars. No
y=this time:so.Count()is a stat that counts rows for eachx,so.Bar()draws them, andso.Dodge()is a move that puts the bars for eachcolorside by side instead of on top of each other. - A histogram is a stat too:
so.Hist(binwidth=200)cuts body mass into bins and counts them, andso.Bars()draws them as touching bars..facet(col="species")splits it into panels, one per species, and.label()names the axes. - Dots and lines. One layer of dots and one of
so.Line()withso.PolyFit(order=1), a stat that fits a straight line. Becausecolor="species"is in thePlot, both layers use it: three fitted lines, one per species..scale(color=[...])picks the colours (Okabe and Ito's, from 6.7), and.label(title=...)adds a title.
Each new chart was a new combination, not a new function. That's the point of the grammar: if you can say what you want in its words (count the rows per species and sex, draw bars, dodge them), you can write it. The function-per-chart interface from 7.1 to 7.6 is quicker for the usual charts, and has more of them ready-made; reach for so.Plot when the chart you want isn't one of them, or has layers.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Wilkinson, The Grammar of Graphics, 2nd ed., Springer, 2005: data, mappings, marks, stats, scales and facets as the parts of every chart
- seaborn: The seaborn.objects interface: Plot, marks, stats, moves, facets and scales
- Wickham, A Layered Grammar of Graphics, Journal of Computational and Graphical Statistics 19(1), 2010: layers: the grammar as ggplot2 uses it, and seaborn.objects after it