Estimating Means with Bar Plots
A barplot represents an estimate of the central tendency (usually the mean) for a numeric variable with the height of each rectangle.
Unlike a histogram which counts how many items are in a bin, a barplot calculates a statistic (like the average bill amount) for each category.
The Error Bars (Uncertainty)
The small black lines on top of each bar are called error bars. By default, they show the 95% Confidence Interval.
To customize them, we now use the err_kws (error keywords) parameter. This accepts a dictionary of settings that control the look of these lines.
Key Parameters
capsize: adds a small horizontal line ("cap") to the ends of the error bars;err_kws: a dictionary to customize error bars;{'color': 'black'}: sets the color;{'linewidth': 2}: sets the thickness;estimator: by default, it calculates the mean. You can change this tomedian,sum, ormax(requires importingnumpy).
Example
Here is a barplot showing the average total bill with customized red error bars.
12345678910111213141516171819import seaborn as sns import matplotlib.pyplot as plt # Load dataset df = sns.load_dataset('tips') # Create a barplot sns.barplot( data=df, x='day', y='total_bill', hue='sex', capsize=0.1, # New way to style error bars: err_kws={'color': 'red', 'linewidth': 2}, palette='pastel' ) plt.show()
Swipe to start coding
Calculate and visualize the average total bill for different days, comparing smokers and non-smokers.
- Set the style to
'ticks'to remove the grid. - Create a barplot using the
tipsdataset (df):- Map
'day'toxand'total_bill'toy. - Group by
'smoker'usinghue. - Set the error bar
capsizeto0.1. - Change the error bar color to
'pink'using theerr_kwsdictionary (e.g.,{'color': 'pink'}). - Set the bar outline
linewidthto2.5. - Use the
'magma'palette.
- Map
- Display the plot.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4.55
Estimating Means with Bar Plots
Swipe to show menu
A barplot represents an estimate of the central tendency (usually the mean) for a numeric variable with the height of each rectangle.
Unlike a histogram which counts how many items are in a bin, a barplot calculates a statistic (like the average bill amount) for each category.
The Error Bars (Uncertainty)
The small black lines on top of each bar are called error bars. By default, they show the 95% Confidence Interval.
To customize them, we now use the err_kws (error keywords) parameter. This accepts a dictionary of settings that control the look of these lines.
Key Parameters
capsize: adds a small horizontal line ("cap") to the ends of the error bars;err_kws: a dictionary to customize error bars;{'color': 'black'}: sets the color;{'linewidth': 2}: sets the thickness;estimator: by default, it calculates the mean. You can change this tomedian,sum, ormax(requires importingnumpy).
Example
Here is a barplot showing the average total bill with customized red error bars.
12345678910111213141516171819import seaborn as sns import matplotlib.pyplot as plt # Load dataset df = sns.load_dataset('tips') # Create a barplot sns.barplot( data=df, x='day', y='total_bill', hue='sex', capsize=0.1, # New way to style error bars: err_kws={'color': 'red', 'linewidth': 2}, palette='pastel' ) plt.show()
Swipe to start coding
Calculate and visualize the average total bill for different days, comparing smokers and non-smokers.
- Set the style to
'ticks'to remove the grid. - Create a barplot using the
tipsdataset (df):- Map
'day'toxand'total_bill'toy. - Group by
'smoker'usinghue. - Set the error bar
capsizeto0.1. - Change the error bar color to
'pink'using theerr_kwsdictionary (e.g.,{'color': 'pink'}). - Set the bar outline
linewidthto2.5. - Use the
'magma'palette.
- Map
- Display the plot.
Solution
Thanks for your feedback!
single