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.
flowchart LR A["All rows for
one category"] --> B["Aggregate
(mean by default)"] B --> C["Draw one bar"] A --> D["Estimate uncertainty"] D --> E["Draw error bar
(95% confidence interval)"]
Example: mean total bill by day
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
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.)
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:
Tips
- Use
barplotfor comparing group summaries. - Use
countplotwhen you want raw counts. - If you already aggregated your data with Pandas, consider Matplotlib bars.
Check your understanding
What does sns.barplot(data=tips, x="day", y="total_bill") draw by default for each day?
barplot aggregates with the mean by default and draws a confidence interval on top. Sums and counts need a custom estimator or countplot.
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.
How do you make barplot show the median total bill per day instead of the mean?
The estimator argument takes the aggregate function. hue splits by a category, and order only controls category order.
Show answer
C — estimator=np.median — The estimator argument takes the aggregate function. hue splits by a category, and order only controls category order.
What does adding hue="sex" to the barplot do?
hue adds a second grouping, so each day gets one bar per sex. It does not filter or change the estimator.
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.
What does the whisker on top of each bar represent?
The whisker shows how much the mean might wobble if you resampled the data, not the raw maximum.
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
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 – Split Each Bar by Time
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 – Change the Estimator
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.