Skip to content

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.

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()

Tips

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

If this helped you, consider buying me a coffee โ˜•

Buy me a coffee

Was this page helpful?

Let us know how we did