Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 38 ยท Relational and Categorical Plots

Bar, Count and Point Plots

Draw summaries per category with seaborn barplot, countplot and pointplot: means with confidence intervals, counts, hue groups, ordering, labels and pitfalls.

Bar, count and point plots reduce each category to one number: a mean, a count or a proportion. They are the fastest charts to read and the ones most people expect in reports. Their danger is that a single number hides the distribution behind it, so know exactly what each function computes.

barplot: the mean with an interval

sns.barplot draws one bar per category, with height equal to the mean of the numeric variable (by default) and a thin line for the 95% confidence interval of that mean.

Python barplot with hue and error bars

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.

Arguments worth knowing:

  • estimator="mean" (default), or "median", "sum", "max", len, or any function.
  • errorbar=("ci", 95) (default), "sd", ("se", 2), ("pi", 50) or None. The meaning of each is in Error Bars and Confidence Intervals.
  • capsize=0.2 adds caps to the interval lines.
  • n_boot sets the number of bootstrap samples; seed makes the intervals reproducible.
  • err_kws={"linewidth": 1} styles them; edgecolor, width, saturation and palette style the bars.
  • order, hue_order: ordering. dodge=False overlays hue groups.
  • orient="h" (or swap x and y) for horizontal bars, which suits long labels.

Bars encode value by length from zero, so keep the axis starting at zero. A bar chart with a truncated axis exaggerates small differences.

To label values, add ax.bar_label(ax.containers[0], fmt="%.1f") (one call per container, if you use hue there are several).

countplot: how many rows?

sns.countplot does not need a y variable. It counts the rows in each category.

Python countplot and normalized counts

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.

stat can be "count" (default), "percent", "proportion" or "probability". Sorting by frequency with value_counts().index makes ranked charts easy to read. For a category of rates, such as the share of a group that converted, bars of mean (barplot on a 0/1 column) show the proportion with a confidence interval.

pointplot: points joined by lines

sns.pointplot shows the same statistic as barplot (mean with a 95% interval), but draws a dot with an interval and joins the dots with a line. Points need not start at zero, so small differences between groups, and how they change over a second variable, are easier to see.

Python pointplot for interaction patterns

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 two lines are not parallel: laptop engagement climbs through the day while phone stays flat. Non-parallel lines suggest an interaction (the effect of time of day depends on the device). dodge=0.3 offsets the two groups so their intervals do not overlap. The lines connect group means; they do not imply values in between, and they make sense mainly for ordered categories (times, doses, years).

Choosing among the three

Plot Shows Axis starts at zero? Best for
barplot Mean (or other) per category Yes Comparing sizes, absolute levels
countplot Row counts per category Yes Frequencies, class balance
pointplot Mean per category, linked No Trends across ordered conditions, interactions

Pitfalls

  • Mean of skewed data: a bar of means can be dominated by outliers. Check a box or violin plot, or use estimator="median".
  • Hidden sample sizes: a bar with 3 rows and one with 3,000 look alike. Show counts nearby.
  • Intervals are not the data range: the line is the confidence interval of the mean, not of individual values.
  • Percent of what? countplot(stat="percent") is percent of all rows, not of each category's rows.
  • Too many categories: sort by value and go horizontal.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the bar height represent in sns.barplot by default?

    barplot aggregates with the mean unless you change estimator.
  2. 2. Which function takes only one categorical variable and counts rows?

    countplot does the counting itself; no y variable is needed.
  3. 3. When is pointplot preferable to barplot?

    Points need no zero baseline and the connecting lines make patterns and interactions visible.
  4. 4. What does the thin line on a seaborn bar represent by default?

    It shows uncertainty in the mean, not the spread of the individual values.
  5. 5. How do you sort countplot bars from most to least frequent?

    Pass the categories in descending frequency order with order=.

Summary

  • barplot shows an aggregated value (mean by default) with a confidence interval; bars should start at zero.
  • countplot counts rows per category and supports stat="percent".
  • pointplot shows the same estimates as dots joined by lines, ideal for trends and interactions across ordered categories.
  • Control the statistic with estimator and the interval with errorbar; sort with order.
  • Summaries hide the distribution, so check a box, violin or swarm plot too.

Next: Regression Plots with regplot and lmplot