Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Estimating Means with Bar Plots | Section
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Statistical Visualization with Seaborn

bookEstimating 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 to median, sum, or max (requires importing numpy).

Example

Here is a barplot showing the average total bill with customized red error bars.

12345678910111213141516171819
import 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()
copy
Task

Swipe to start coding

Calculate and visualize the average total bill for different days, comparing smokers and non-smokers.

  1. Set the style to 'ticks' to remove the grid.
  2. Create a barplot using the tips dataset (df):
    • Map 'day' to x and 'total_bill' to y.
    • Group by 'smoker' using hue.
    • Set the error bar capsize to 0.1.
    • Change the error bar color to 'pink' using the err_kws dictionary (e.g., {'color': 'pink'}).
    • Set the bar outline linewidth to 2.5.
    • Use the 'magma' palette.
  3. Display the plot.

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 13
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

close

bookEstimating 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 to median, sum, or max (requires importing numpy).

Example

Here is a barplot showing the average total bill with customized red error bars.

12345678910111213141516171819
import 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()
copy
Task

Swipe to start coding

Calculate and visualize the average total bill for different days, comparing smokers and non-smokers.

  1. Set the style to 'ticks' to remove the grid.
  2. Create a barplot using the tips dataset (df):
    • Map 'day' to x and 'total_bill' to y.
    • Group by 'smoker' using hue.
    • Set the error bar capsize to 0.1.
    • Change the error bar color to 'pink' using the err_kws dictionary (e.g., {'color': 'pink'}).
    • Set the bar outline linewidth to 2.5.
    • Use the 'magma' palette.
  3. Display the plot.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 1. ChapterΒ 13
single

single

some-alt