Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 38 ยท Statistical Estimation

Estimators and Aggregation: barplot, pointplot and Bubbles

Learn how seaborn summarises data with estimators: mean, median, sum and custom functions in barplot and pointplot, plus weighted bubble plots and countplot.

When you give barplot() a column of raw values with repeated categories, you do not get one bar per row. You get one bar per category, and its height is a summary of the rows in that category. The function that does the summarising is the estimator, and by default it is the mean. Knowing this saves you from the classic surprise: "why is my bar not the total I expected?"

This lesson covers the estimator system in barplot, pointplot and lineplot, how it differs from countplot, and how to encode a third variable in bubble size.

Aggregate first, or let seaborn aggregate

There are two workflows:

  • Raw rows in, seaborn summarises. Pass the unaggregated DataFrame. You get the estimator and the error bars automatically.
  • Pre-aggregated in, seaborn draws. Use groupby().agg() yourself and plot with errorbar=None. Use this when you want totals, ratios or anything the estimator cannot express.

Both are valid; just know which one you are in.

Python The default estimator is the mean

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The same data tells two stories. West has the highest average revenue but the lowest total, because it has the fewest rows. If you read the mean chart as "who earns most overall", you will be wrong. Pick the estimator that answers your question, and say which one it is in the title.

Passing an estimator

estimator accepts a string name that pandas understands ("mean", "median", "sum", "min", "max", "std", "count"), or any function that turns a Series into one number, such as np.median, or lambda v: (v > 300).mean() for a proportion:

Python A custom estimator: share above a threshold

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The bootstrap resamples the raw rows and recomputes the estimator, so the interval works for your custom function too. ax.bar_label writes values above the bars.

pointplot: dots and lines for comparing means

pointplot() shows the same estimate as a dot with an interval, connecting dots across hue levels or x values with a line. It focuses attention on the difference between groups rather than on the bar area, and it does not force a zero baseline, which makes small differences visible. Use it when you compare how an effect changes across conditions:

Python pointplot to show a change across groups

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

dodge offsets the two groups slightly so their error bars do not overlap. The widening gap between the lines is the interaction: the effect of the method depends on the grade, which would be much harder to see in grouped bars.

Bars versus points: use bars for amounts that start at zero (counts, totals), points for comparing means and effects.

countplot: the estimator is "count"

countplot() takes only one axis and counts rows in each category. It is shorthand for a bar plot with the count as height, so it never needs a y. Set stat="percent" or "proportion" to normalise:

Python countplot with percent and hue

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

order=df.device.value_counts().index sorts bars by frequency, the best default for nominal categories. Note that stat="percent" is a percentage of all rows, not within each device; for within-group shares, compute them with pd.crosstab(..., normalize="index") and plot the result.

Bubble plots: size as a third variable

A bubble plot is a scatter plot whose marker area encodes a third number. In seaborn use scatterplot(size=..., sizes=(min, max)). Always keep the area, not the width, proportional to the value, which sizes does:

Python A bubble plot

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Bubbles overlap, so use alpha and a contrasting edge. Do not size more than one variable's worth of information; with both colour and size, keep colours to a few categories.

Which to choose

Question Use
How many rows per category? countplot
Total per category? barplot(estimator="sum", errorbar=None)
Typical value per category with uncertainty? barplot or pointplot (mean or median)
How does an effect change across conditions? pointplot with hue
Relationship plus magnitude? bubble scatterplot

Each estimate gets its interval from the rules in error bars and confidence intervals.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does barplot show by default for each category?

    The default estimator is the mean, and the default error bar is a bootstrapped 95% CI.
  2. 2. How do you plot the total revenue per region from raw rows?

    Set the estimator to sum; a CI of a total is not meaningful, so turn errorbar off.
  3. 3. When is pointplot better than barplot?

    Points and connecting lines emphasise differences between groups.
  4. 4. In a bubble plot, which should be proportional to the value?

    People perceive area, so sizes maps the value to marker area.

Summary

  • barplot, pointplot and lineplot summarise raw rows with an estimator (mean by default); choose it to match your question.
  • Custom estimators work and still get bootstrapped intervals.
  • countplot counts rows; stat gives percent or proportion; order bars by frequency.
  • Bubble plots add a third variable with size and sizes.

Next: Project: Exploring a Penguin-Style Dataset