Bar Plots in Seaborn
What is barplotbarplot in Seaborn?
Seaborn’s barplotbarplot 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
Seaborn barplot
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()Seaborn barplot
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
barplot with 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()barplot with 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.)
Custom estimator
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()Custom estimator
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
barplotbarplotfor comparing group summaries. - Use
countplotcountplotwhen you want raw counts. - If you already aggregated your data with Pandas, consider Matplotlib bars.
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
Exercise 2 – Split Each Bar by Time
Exercise 3 – Change the Estimator
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
