Python Central HubSign in

Data Analytics with Python / Phase 6: Statistical Visualization with Seaborn

Bar Plots in Seaborn

Use Seaborn barplot to show aggregated values with confidence intervals and compare groups.

What is barplot in Seaborn?

Seaborn’s barplot typically plots:

  • An aggregate (default is mean)
  • An uncertainty interval (confidence interval)

This is different from Matplotlib bars where you usually plot raw totals.

diagram How barplot builds one bar mermaid
Every row in a category gets averaged into one bar height, plus a confidence interval line on top.

Example: mean total bill by day

Python
import seaborn as sns
import matplotlib.pyplot as plt
 
tips = sns.load_dataset("tips")
 
plt.figure(figsize=(7, 4))
sns.barplot(data=tips, x="day", y="total_bill")
plt.title("Mean total bill by day")
plt.tight_layout()
plt.show()

Add hue

Python
import seaborn as sns
import matplotlib.pyplot as plt
 
tips = sns.load_dataset("tips")
 
plt.figure(figsize=(7, 4))
sns.barplot(data=tips, x="day", y="total_bill", hue="sex")
plt.title("Mean total bill by day and sex")
plt.tight_layout()
plt.show()

Change estimator (sum, median, etc.)

Python
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
 
tips = sns.load_dataset("tips")
 
plt.figure(figsize=(7, 4))
sns.barplot(data=tips, x="day", y="total_bill", estimator=np.median)
plt.title("Median total bill by day")
plt.tight_layout()
plt.show()

Visualize it

The whisker on top of each bar is the confidence interval — it tells you how much the average might wobble if you resampled the data:

sketch Bars with confidence-interval error bars p5.js
Bar height is the group mean; the whisker on top shows the uncertainty around that mean.

Tips

  • Use barplot for comparing group summaries.
  • Use countplot when you want raw counts.
  • If you already aggregated your data with Pandas, consider Matplotlib bars.

Check your understanding

quizCheck yourself
  1. What does sns.barplot(data=tips, x="day", y="total_bill") draw by default for each day?

    Show answer

    B — The mean of total_bill with a 95% confidence interval — barplot aggregates with the mean by default and draws a confidence interval on top. Sums and counts need a custom estimator or countplot.

  2. How do you make barplot show the median total bill per day instead of the mean?

    Show answer

    C — estimator=np.median — The estimator argument takes the aggregate function. hue splits by a category, and order only controls category order.

  3. What does adding hue="sex" to the barplot do?

    Show answer

    A — Splits each day into side-by-side bars for each sex — hue adds a second grouping, so each day gets one bar per sex. It does not filter or change the estimator.

  4. What does the whisker on top of each bar represent?

    Show answer

    C — The 95% confidence interval around the group mean — The whisker shows how much the mean might wobble if you resampled the data, not the raw maximum.

Next

Continue to Count Plots for a simpler case: bars that just count rows per category, no aggregation needed.

🧪 Try It Yourself

Exercise 1 – Mean by Category

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Split Each Bar by Time

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Change the Estimator

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did