sns.barplot()
Displays the mean (or other aggregate values) of a numerical variable for each category.
Seaborn sns.barplot()
– Categorical Aggregation with Bars
sns.barplot()
is used to visualize the mean (or other aggregate statistics) of a numerical variable for different categorical variables. It is useful for comparisons across categories.
1. General Syntax
python
CopyEdit
sns.barplot(
data=None,
x=None,
y=None,
hue=None,
order=None,
hue_order=None,
estimator=None,
ci=95,
n_boot=1000,
units=None,
seed=None,
orient=None,
color=None,
palette=None,
saturation=0.75,
width=0.8,
errcolor=".26",
errwidth=None,
capsize=None,
dodge=True
)
2. Key Parameters
Parameter | Description |
data | DataFrame or array-like dataset. |
x , y | Categorical and numerical variables to plot. |
hue | Color-coding by a categorical variable. |
estimator | Function applied to aggregate data (default: mean ). |
ci | Confidence interval size (default 95% , set to None to remove). |
n_boot | Number of bootstraps for CI computation (default 1000 ). |
palette | Color mapping for categorical groups. |
width | Controls bar width (default 0.8 ). |
errcolor | Color of error bars (default gray ). |
errwidth | Line width of error bars. |
capsize | Adds caps to error bars. |
dodge | Separates bars when using hue . |
3. Dataset Setup
We will use the titanic
dataset from Seaborn.
python
CopyEdit
import seaborn as sns
import matplotlib.pyplot as plt
# Load dataset
data = sns.load_dataset("titanic")
data.head()
Dataset Columns
- survived: Whether the passenger survived (0 = No, 1 = Yes).
- pclass: Passenger class (1st, 2nd, 3rd).
- sex: Gender.
- age: Age in years.
- fare: Ticket fare.
- embark_town: Port of embarkation.
survived | pclass | sex | age | fare | embark_town |
0 | 3 | male | 22.0 | 7.25 | Southampton |
1 | 1 | female | 38.0 | 71.28 | Cherbourg |
1 | 3 | female | 26.0 | 7.92 | Southampton |
1 | 1 | female | 35.0 | 53.10 | Southampton |
0 | 3 | male | 35.0 | 8.05 | Southampton |
4. Basic Bar Plot
Plot the average fare for each passenger class (pclass
)
python
CopyEdit
sns.barplot(data=data, x="pclass", y="fare")
plt.title("Average Fare by Passenger Class")
plt.show()
Explanation
- The height of the bars represents the average fare.
- Default 95% confidence interval (CI) is shown as error bars.
5. Removing Confidence Intervals (ci=None
)
python
CopyEdit
sns.barplot(data=data, x="pclass", y="fare", ci=None)
plt.title("Bar Plot Without Confidence Intervals")
plt.show()
Explanation
- Removing confidence intervals makes the plot cleaner.
6. Changing the Aggregation Method (estimator
)
Use median
instead of mean
python
CopyEdit
import numpy as np
sns.barplot(data=data, x="pclass", y="fare", estimator=np.median)
plt.title("Median Fare by Passenger Class")
plt.show()
Explanation
- By default,
sns.barplot()
calculates mean values. estimator=np.median
changes the calculation to median.
7. Grouping Data with hue
Compare fares across sex
within each pclass
python
CopyEdit
sns.barplot(data=data, x="pclass", y="fare", hue="sex")
plt.title("Average Fare by Passenger Class and Gender")
plt.show()
Explanation
- Uses different colors for
male
andfemale
groups.
8. Adjusting Bar Width (width
)
python
CopyEdit
sns.barplot(data=data, x="pclass", y="fare", width=0.5)
plt.title("Bar Plot with Narrower Bars")
plt.show()
Explanation
width=0.5
reduces bar width.
9. Changing Color Palettes (palette
)
Use coolwarm
palette
python
CopyEdit
sns.barplot(data=data, x="pclass", y="fare", hue="sex", palette="coolwarm")
plt.title("Custom Color Palette")
plt.show()
Explanation
- Uses Seaborn’s predefined color palettes.
10. Adding Error Bar Caps (capsize
)
python
CopyEdit
sns.barplot(data=data, x="pclass", y="fare", capsize=0.2)
plt.title("Bar Plot with Error Bar Caps")
plt.show()
Explanation
- Caps make error bars easier to interpret.
11. Customizing Error Bars (errwidth
and errcolor
)
python
CopyEdit
sns.barplot(data=data, x="pclass", y="fare", errwidth=2, errcolor="red")
plt.title("Customized Error Bars")
plt.show()
Explanation
- Increases error bar width and changes color to red.
12. Using dodge=False
to Stack Bars
python
CopyEdit
sns.barplot(data=data, x="pclass", y="fare", hue="sex", dodge=False)
plt.title("Stacked Bars Instead of Side-by-Side")
plt.show()
Explanation
- Prevents bars from being separated when using
hue
.
13. Horizontal Bar Plot
python
CopyEdit
sns.barplot(data=data, y="pclass", x="fare")
plt.title("Horizontal Bar Plot of Fare by Passenger Class")
plt.show()
Explanation
- Swaps
x
andy
to create a horizontal bar plot.
14. Rotating X-axis Labels
python
CopyEdit
sns.barplot(data=data, x="embark_town", y="fare")
plt.xticks(rotation=45)
plt.title("Rotated X-axis Labels")
plt.show()
Explanation
- Prevents overlapping labels on the x-axis.
15. Adjusting Figure Size
python
CopyEdit
plt.figure(figsize=(10, 6))
sns.barplot(data=data, x="pclass", y="fare")
plt.title("Larger Figure Size")
plt.show()
Explanation
- Improves readability for large datasets.
16. Final Example: Fully Customized Bar Plot
python
CopyEdit
plt.figure(figsize=(12, 6))
sns.barplot(
data=data,
x="pclass",
y="fare",
hue="sex",
palette="viridis",
capsize=0.1,
errwidth=2,
width=0.7
)
plt.title("Customized Bar Plot of Fare by Passenger Class and Gender")
plt.xlabel("Passenger Class")
plt.ylabel("Average Fare")
plt.xticks(rotation=0)
plt.grid(axis="y", linestyle="--", alpha=0.7)
plt.show()
Features in This Plot
✔ Hue by sex
✔ Caps on error bars
✔ Custom width and error bar thickness
✔ Larger figure size
✔ Grid lines for clarity
Conclusion
✅ sns.barplot()
is a great tool for visualizing category-based aggregation.
✅ It provides clear comparisons using means, medians, or other statistical measures.
✅ Highly customizable, supporting hue-based grouping, confidence intervals, color palettes, error bars, and grid styling.
Mastering bar plots will enhance your ability to present and compare numerical data effectively! 🚀