seaborn and interactive charts · lesson 7 of 10
Parts in this lesson
Aimport seaborn.objects as sothe objects interface
Bso.Plot(df, x="a", y="b")data and mapping: what goes where
C.add(so.Dot(), so.Jitter())a layer: a mark, and stats or moves
D.facet(col="g").scale(...).label(...)panels, how values become looks, and titles

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.

PartWhat it isIn the chart below
Datathe tablethe penguins
Mappingwhich column goes to which aestheticspecies across, body mass up
Markwhat's drawn for each row or summarydots, a dash
Stata calculation on the data before drawingthe mean of each species
Movea shift so marks don't hide each otherjitter, dodge
Scalehow a value becomes a position, colour or sizewhich colour each species gets
Facetsplitting 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:

Grammar builder. Data and mapping. 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.AdelieGentooChinstrap3,0004,0005,0006,000speciesbody_mass_g
1 of 5 Data and mapping. 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.

grammar.ipynb
  • 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 .add returns a new Plot, 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 a so.Dash mark. The stat is a separate part from the mark: so.Agg("median") would give a median dash, and so.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.

grammar.ipynb
  • Counted bars. No y= this time: so.Count() is a stat that counts rows for each x, so.Bar() draws them, and so.Dodge() is a move that puts the bars for each color side 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, and so.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() with so.PolyFit(order=1), a stat that fits a straight line. Because color="species" is in the Plot, 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

In .add(so.Line(), so.PolyFit(order=1)), which part is the stat?
Your dodged bars should be stacked instead. Which part of the grammar do you change?
What does so.Plot(penguins, x="species", y="body_mass_g") draw on its own?

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.

All sources for Simple Data Analysis