seaborn and interactive charts · lesson 3 of 10
Parts in this lesson
Asns.boxplot(data=df, x="g", y="a")median, quartiles and outliers per group
Bsns.stripplot(...) / sns.swarmplot(...)every value as a dot
Csns.barplot(..., errorbar="sd")a mean per group, with an error bar you choose
Dsns.pointplot(..., hue="h", dodge=0.3)means as points, joined across groups

Comparing groups

Last updated

Which species is heaviest? Which store sells most on a typical day? Comparing groups is one of the commonest jobs for a chart, and one of the easiest to do dishonestly by accident. In this lesson you'll draw the same comparison four ways, learn what a bar chart's error bars really mean, and see a bar of averages hide that one group has only three numbers behind it.

By the end of this lesson you can

  • Compare groups with box, violin, strip and swarm plots, and say what each shows
  • Say what a barplot’s error bars mean: a confidence interval or a standard deviation
  • Show the raw points behind a summary, so small or odd groups can’t hide

Four ways to compare

Run the cell: the flipper lengths of the three species, drawn four ways in one figure.

flippers.ipynb
  • boxplot: the box runs from the first to the third quartile (the middle half of the penguins), the line inside is the median, the whiskers reach the furthest values within 1.5 box-lengths, and anything further out is a dot. Compact and easy to compare, but it can't show two humps.
  • violinplot: a KDE curve (7.2) drawn on both sides, with a small box plot inside. It shows the shape, at the cost of smoothing.
  • stripplot: every penguin as a dot, nudged sideways at random (jitter) so they don't all sit on one line.
  • swarmplot: every penguin as a dot, nudged sideways just enough that none overlap, so the width shows how many there are at each length. It works up to a few hundred points per group; with thousands, use a box or violin.

All four agree here: Gentoos have the longest flippers, by more than 20 mm, and Adelies and Chinstraps overlap a lot. When groups are small, show the points; when they're big, show a summary; often, show both (step 3).

What the error bars mean

A bar plot shows one number per group, by default the mean, with a black line, the error bar, on top. What that line means depends on errorbar=, and a reader can't tell from the chart, so you must say. Run the cells.

means.ipynb
  • The table has what the bars are made from: each species' mean, standard deviation and count.
  • errorbar=("ci", 95) (the default) draws a 95% confidence interval for the mean, found by resampling the data 1,000 times (a bootstrap; Stage 8 explains it). It answers how sure are we of the mean? It's short here because there are dozens of penguins per species. seed=1 makes the resampling the same on every run.
  • errorbar="sd" draws the mean plus and minus one standard deviation. It answers how spread out are the penguins?, and it's much longer: Adelies' standard deviation is 459 g.
  • The last cell uses pointplot: a dot per mean, joined by lines, with hue="species" for one line per species and dodge=0.3 to shift them apart so their error bars don't overlap. Point plots are good for comparing many groups, and lines make it easy to see a pattern repeated across islands. Chinstraps live only on Dream and Gentoos only on Biscoe, which is why those have one point each.

Put what the bars mean in the title or a note, every time: “mean ± 1 standard deviation”.

Wrong way: three days behind a bar

A bar shows one number. What it can't show is how many values made it, or what they looked like. Step through three groups with the same bar:

Behind the bar. Three bars from sns.barplot(..., errorbar="sd"). Each group has a mean of 20 and a standard deviation of 8, so the bars and their error bars are identical.group Agroup Bgroup C010203040value
1 of 3 Three bars from sns.barplot(..., errorbar="sd"). Each group has a mean of 20 and a standard deviation of 8, so the bars and their error bars are identical.

Now the same trap with Tindahan data. A fifth store has just opened in Calamba. Run the cells.

stores.ipynb

The first chart says Calamba sells the most kilos a day. The table says why: Batangas' bar is the mean of 60 days, and Calamba's of 3. Calamba's three days were 92, 31.5 and 74 kilos; another day like its 31.5 would drop it below Batangas.

The third cell draws the bars in pale grey and puts every day on top with stripplot, passing the same ax. Now the reader can see at once that Calamba's bar stands on three dots, and that its days are far more uneven than any other store's.

The check: for every bar chart of averages, ask how many values are behind each bar. If the groups are small or different sizes, show the points, or at least write the counts on the chart.

Check what you learned

Which chart could show that a group’s values fall in two separate clumps?
A report shows bars of mean spend per customer, with short error bars, and no note. What can’t you tell?
One group in your bar chart has 4 values and the others have 200. What should you do?

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