0
0
Matplotlibdata~30 mins

Error bars on bar charts in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Error bars on bar charts
📖 Scenario: You work in a small bakery that tracks daily sales of different types of bread. You want to show the average sales for each bread type along with how much the sales vary each day.
🎯 Goal: Create a bar chart showing average sales for each bread type with error bars representing the daily sales variation.
📋 What You'll Learn
Create a dictionary called daily_sales with bread types as keys and lists of daily sales numbers as values.
Create a variable called average_sales that stores the average sales for each bread type.
Create a variable called sales_std that stores the standard deviation of sales for each bread type.
Use matplotlib to plot a bar chart with error bars using average_sales and sales_std.
Display the bar chart with labels for bread types and a title.
💡 Why This Matters
🌍 Real World
Bakers and shop owners use sales charts with error bars to understand average sales and variability, helping with inventory and production planning.
💼 Career
Data analysts and business intelligence professionals often create charts with error bars to communicate data variability clearly to stakeholders.
Progress0 / 4 steps
1
Create the daily sales data
Create a dictionary called daily_sales with these exact entries: 'Sourdough': [20, 22, 19, 24, 21], 'Baguette': [15, 14, 16, 15, 17], 'Rye': [10, 9, 11, 10, 12].
Matplotlib
Need a hint?

Use a dictionary with bread names as keys and lists of numbers as values.

2
Calculate average sales and standard deviation
Create two dictionaries: average_sales and sales_std. Use a for loop with variables bread and sales to iterate over daily_sales.items(). Calculate the average sales using sum(sales) / len(sales) and the standard deviation using the formula for sample standard deviation.
Matplotlib
Need a hint?

Use a loop to calculate average and standard deviation for each bread type.

3
Plot the bar chart with error bars
Import matplotlib.pyplot as plt. Create a list bread_types from the keys of average_sales. Create a list averages from the values of average_sales. Create a list errors from the values of sales_std. Use plt.bar with bread_types, averages, and yerr=errors to plot the bar chart with error bars.
Matplotlib
Need a hint?

Use plt.bar with yerr to add error bars.

4
Display the bar chart
Use plt.show() to display the bar chart with error bars.
Matplotlib
Need a hint?

Use plt.show() to display the chart window.