When analyzing categorical data, a common task is to compare a numerical measure across different categories. For instance, you might want to compare the average sales figures for different product types, or the mean test scores for various teaching methods. Seaborn's barplot
function is specifically designed for this purpose. It calculates an aggregate statistic (like the mean) for a numerical variable within each category and displays it using rectangular bars.
seaborn.barplot
The seaborn.barplot()
function goes beyond simply drawing bars based on counts. Its primary role is to calculate and plot a measure of central tendency (by default, the mean) for a quantitative variable, grouped by the levels of one or more categorical variables. Crucially, it also visualizes the uncertainty around that estimate using error bars.
Let's look at how to use it. The basic syntax involves specifying the categorical variable for one axis (usually x
), the numerical variable for the other axis (usually y
), and the DataFrame containing the data using the data
parameter.
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
# Load a sample dataset (comes with Seaborn)
tips = sns.load_dataset("tips")
# Create a bar plot showing the average total bill for each day
plt.figure(figsize=(8, 5)) # Adjust figure size for better readability
sns.barplot(x="day", y="total_bill", data=tips)
plt.title("Average Total Bill per Day")
plt.xlabel("Day of the Week")
plt.ylabel("Average Total Bill ($)")
plt.show()
The resulting plot shows the average 'total_bill' for each 'day'. The height of each bar represents the calculated mean.
By default, barplot
calculates the mean of the numerical variable for each category. However, you might be interested in a different aggregate statistic, like the median, which is less sensitive to outliers. You can control this using the estimator
parameter. This parameter accepts a function that computes the desired statistic. Common choices include numpy.mean
(the default), numpy.median
, numpy.std
(standard deviation), etc.
Let's plot the median total bill per day instead:
# Create a bar plot showing the median total bill for each day
plt.figure(figsize=(8, 5))
sns.barplot(x="day", y="total_bill", data=tips, estimator=np.median)
plt.title("Median Total Bill per Day")
plt.xlabel("Day of the Week")
plt.ylabel("Median Total Bill ($)")
plt.show()
Comparing this plot to the previous one might reveal differences, especially if the distribution of total_bill
within any given day is skewed.
You'll notice vertical lines on top of the bars in the plots above. These are error bars, and they provide important context about the uncertainty or variability in the calculated aggregate statistic. By default, barplot
displays 95% confidence intervals for the mean.
A confidence interval gives an estimated range of values which is likely to include the true population mean. A smaller error bar suggests less variability and higher confidence in the point estimate (the bar height), while a larger error bar indicates more variability or less data, making the estimate less precise. Comparing error bars between categories is important; if the error bars for two categories overlap significantly, it suggests that the difference between their means might not be statistically significant.
The calculation method and size of the error bars can be controlled using the errorbar
parameter (which replaces the older ci
parameter). Common options include:
('ci', 95)
: Show 95% confidence intervals (default). You can change the percentage (e.g., 99).'sd'
: Show the standard deviation of the data within each category.None
: Do not draw error bars.Here's how to plot the mean total bill with error bars representing the standard deviation:
# Create a bar plot with standard deviation error bars
plt.figure(figsize=(8, 5))
sns.barplot(x="day", y="total_bill", data=tips, errorbar='sd') # Use standard deviation
plt.title("Average Total Bill per Day (with Standard Deviation)")
plt.xlabel("Day of the Week")
plt.ylabel("Average Total Bill ($)")
plt.show()
Notice how the error bars now represent the spread (standard deviation) of the bills for each day, rather than the confidence in the mean estimate.
Sometimes, especially if you have many categories or long category names, a horizontal bar plot is more readable. You can easily create one by assigning the categorical variable to the y
axis and the numerical variable to the x
axis. Seaborn infers the orientation automatically.
# Create a horizontal bar plot
plt.figure(figsize=(7, 5))
sns.barplot(x="total_bill", y="day", data=tips, estimator=np.mean, errorbar=('ci', 95))
plt.title("Average Total Bill per Day")
plt.xlabel("Average Total Bill ($)")
plt.ylabel("Day of the Week")
plt.show()
barplot
can also show aggregates broken down by two categorical variables. You can achieve this using the hue
parameter. This creates grouped bars, where bars for different levels of the hue
variable appear side-by-side within each category on the main axis.
Let's see the average total bill per day, further broken down by whether the customer was a smoker:
# Create a grouped bar plot
plt.figure(figsize=(10, 6))
sns.barplot(x="day", y="total_bill", hue="smoker", data=tips)
plt.title("Average Total Bill per Day by Smoker Status")
plt.xlabel("Day of the Week")
plt.ylabel("Average Total Bill ($)")
plt.show()
Example showing the average total bill per day, with error bars representing the 95% confidence interval for the mean.
In summary, seaborn.barplot
is a powerful tool for comparing an aggregate measure (like the mean or median) of a numerical variable across different categories. The inclusion of error bars adds valuable information about the uncertainty or variability associated with these estimates, enabling more informed comparisons. Remember that barplot
shows an aggregate statistic, differentiating it from countplot
, which simply shows the number of observations in each category (covered next).
© 2025 ApX Machine Learning