Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
孊ぶ Estimating Means with Bar Plots | Section
Statistical Visualization with Seaborn

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 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()
タスク

スワむプしおコヌディングを開始

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.

解答

Switch to desktop実践的な緎習のためにデスクトップに切り替える䞋蚘のオプションのいずれかを利甚しお、珟圚の堎所から続行する
すべお明確でしたか

どのように改善できたすか

フィヌドバックありがずうございたす

セクション 1. 章 13
single

single

AIに質問する

expand

AIに質問する

ChatGPT

䜕でも質問するか、提案された質問の1぀を詊しおチャットを始めおください

some-alt