0
0
Matplotlibdata~30 mins

Why bar charts compare categories in Matplotlib - See It in Action

Choose your learning style9 modes available
Why Bar Charts Compare Categories
📖 Scenario: Imagine you run a small bakery. You want to see which types of cakes sell the most each day. You have sales numbers for three cake types: Chocolate, Vanilla, and Strawberry.
🎯 Goal: You will create a bar chart to compare the sales of these cake types. This will help you see which cake is the most popular at a glance.
📋 What You'll Learn
Create a dictionary called cake_sales with exact entries: 'Chocolate': 30, 'Vanilla': 45, 'Strawberry': 25
Create a list called categories that contains the keys from cake_sales
Create a list called sales that contains the values from cake_sales
Use matplotlib.pyplot to create a bar chart with categories on the x-axis and sales on the y-axis
Add a title to the chart: 'Cake Sales Comparison'
Label the x-axis as 'Cake Type' and the y-axis as 'Number Sold'
Display the bar chart
💡 Why This Matters
🌍 Real World
Bar charts help businesses quickly compare sales or counts across different categories, like product types or customer groups.
💼 Career
Data analysts and scientists use bar charts to communicate insights clearly to teams and decision makers.
Progress0 / 4 steps
1
Create the cake sales data
Create a dictionary called cake_sales with these exact entries: 'Chocolate': 30, 'Vanilla': 45, 'Strawberry': 25
Matplotlib
Need a hint?

Use curly braces {} to create a dictionary with keys and values.

2
Prepare categories and sales lists
Create a list called categories that contains the keys from cake_sales. Then create a list called sales that contains the values from cake_sales.
Matplotlib
Need a hint?

Use list() with cake_sales.keys() and cake_sales.values() to get lists.

3
Create the bar chart
Import matplotlib.pyplot as plt. Use plt.bar() with categories and sales to create a bar chart. Add the title 'Cake Sales Comparison'. Label the x-axis as 'Cake Type' and the y-axis as 'Number Sold'.
Matplotlib
Need a hint?

Use plt.bar() to create bars, and plt.title(), plt.xlabel(), plt.ylabel() to add text.

4
Show the bar chart
Use plt.show() to display the bar chart.
Matplotlib
Need a hint?

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