Bar Plots in Seaborn
What is barplot in Seaborn?
Section titled “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
Section titled “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
Section titled “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.)
Section titled “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
Section titled “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:
- Use
barplotfor comparing group summaries. - Use
countplotwhen you want raw counts. - If you already aggregated your data with Pandas, consider Matplotlib bars.
Continue to Count Plots for a simpler case: bars that just count rows per category, no aggregation needed.
🧪 Try It Yourself
Section titled “🧪 Try It Yourself”Exercise 1 – Mean by Category
Section titled “Exercise 1 – Mean by Category”Exercise 2 – Split Each Bar by Time
Section titled “Exercise 2 – Split Each Bar by Time”Exercise 3 – Change the Estimator
Section titled “Exercise 3 – Change the Estimator”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading