📖 Scenario: You work in a small bakery. You want to show how many cakes you sold each day last week. A simple bar chart will help you see which days were busiest.
🎯 Goal: Create a vertical bar chart using plt.bar to display the number of cakes sold each day of the week.
📋 What You'll Learn
Create a list called days with the days: 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'.
Create a list called cakes_sold with the exact numbers: 20, 35, 30, 35, 27, 40, 45.
Use plt.bar to make a vertical bar chart with days on the x-axis and cakes_sold on the y-axis.
Add labels for the x-axis as 'Days' and y-axis as 'Number of Cakes Sold'.
Add a title 'Cakes Sold Last Week'.
Display the chart with plt.show().
💡 Why This Matters
🌍 Real World
Bar charts are a simple way to show comparisons, like sales over days or months, helping businesses see trends quickly.
💼 Career
Data analysts and scientists often use bar charts to present data clearly to teams and decision makers.
Progress0 / 4 steps
1
Create the data lists
Create a list called days with these exact values: 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'. Then create a list called cakes_sold with these exact numbers: 20, 35, 30, 35, 27, 40, 45.
Matplotlib
Hint
Use square brackets to create lists. Separate items with commas.
2
Import matplotlib and set up the plot
Import matplotlib.pyplot as plt. This will let you create the bar chart.
Matplotlib
Hint
Use import matplotlib.pyplot as plt to import the plotting library.
3
Create the vertical bar chart
Use plt.bar with days as the x values and cakes_sold as the heights of the bars. Then add x-axis label 'Days', y-axis label 'Number of Cakes Sold', and the title 'Cakes Sold Last Week'.
Matplotlib
Hint
Use plt.bar(x, y) to create bars. Use plt.xlabel(), plt.ylabel(), and plt.title() to add labels and title.
4
Display the bar chart
Use plt.show() to display the bar chart you created.
Matplotlib
Hint
Use plt.show() to see the chart window pop up.
Practice
(1/5)
1. What does the plt.bar() function do in matplotlib?
easy
A. It creates a scatter plot with dots.
B. It creates a line chart connecting data points.
C. It creates a pie chart showing proportions.
D. It creates a vertical bar chart using categories and their values.
Solution
Step 1: Understand the purpose of plt.bar()
The plt.bar() function is designed to draw vertical bars representing data values for different categories.
Step 2: Compare with other chart types
Other functions like plt.plot() create line charts, plt.scatter() creates scatter plots, and plt.pie() creates pie charts, so they are different from plt.bar().
Final Answer:
It creates a vertical bar chart using categories and their values. -> Option D
Quick Check:
plt.bar() = vertical bar chart [OK]
Hint: Remember: bar = vertical bars for categories [OK]
Common Mistakes:
Confusing bar chart with line or pie charts
Thinking plt.bar() draws horizontal bars
Mixing up plt.bar() with scatter plot functions
2. Which of the following is the correct syntax to create a vertical bar chart with categories ['A', 'B', 'C'] and values [5, 7, 3]?
easy
A. plt.bar(['A', 'B', 'C'], [5, 7, 3])
B. plt.bar([5, 7, 3], ['A', 'B', 'C'])
C. plt.bar('A', 5, 'B', 7, 'C', 3)
D. plt.bar([5, 7, 3])
Solution
Step 1: Identify correct parameter order for plt.bar()
The first argument should be the categories (x-axis labels), and the second argument should be the heights (values).
Step 2: Check each option
plt.bar(['A', 'B', 'C'], [5, 7, 3]) uses categories first and values second, which is correct. plt.bar([5, 7, 3], ['A', 'B', 'C']) reverses them, which is wrong. plt.bar('A', 5, 'B', 7, 'C', 3) uses multiple arguments incorrectly. plt.bar([5, 7, 3]) misses categories.
Final Answer:
plt.bar(['A', 'B', 'C'], [5, 7, 3]) -> Option A
Quick Check:
Categories first, values second in plt.bar() [OK]
Hint: Categories go first, values second in plt.bar() [OK]
Common Mistakes:
Swapping categories and values order
Passing multiple separate arguments instead of lists
Omitting categories or values
3. What will be the height of the bar labeled 'B' in this code?
A. The categories list has fewer items than values list.
B. plt.bar() requires numeric categories, not strings.
C. The values list has fewer items than categories list.
D. plt.show() is missing.
Solution
Step 1: Compare lengths of categories and values
The categories list has 3 items ['X', 'Y', 'Z'], but values list has only 2 items [3, 6]. This mismatch causes an error.
Step 2: Understand plt.bar() requirements
Each category must have a corresponding value. Since values are fewer, matplotlib will raise a ValueError.
Final Answer:
The values list has fewer items than categories list. -> Option C
Quick Check:
Lengths must match: values shorter than categories [OK]
Hint: Check categories and values list lengths match [OK]
Common Mistakes:
Ignoring length mismatch between categories and values
Thinking categories must be numeric
Forgetting plt.show()
5. You want to create a vertical bar chart showing sales for products ['Pen', 'Notebook', 'Eraser'] with sales [10, 15, 10]. You also want to highlight the product with the highest sales in red and others in blue. Which code correctly achieves this?
hard
A. plt.bar(['Pen', 'Notebook', 'Eraser'], [10, 15, 10], color='red')
plt.show()
D. plt.bar(['Pen', 'Notebook', 'Eraser'], [10, 15, 10])
plt.show()
Solution
Step 1: Identify highest sales product
The sales are [10, 15, 10], so 'Notebook' has the highest sales (15).
Step 2: Assign colors to highlight highest sales
We want 'Notebook' bar in red and others in blue. colors = ['blue', 'red', 'blue']
plt.bar(['Pen', 'Notebook', 'Eraser'], [10, 15, 10], color=colors)
plt.show() sets colors as ['blue', 'red', 'blue'], matching the order of products.
Step 3: Check other options
plt.bar(['Pen', 'Notebook', 'Eraser'], [10, 15, 10], color='red')
plt.show() colors all bars red, not highlighting only highest. colors = ['red', 'blue', 'blue']
plt.bar(['Pen', 'Notebook', 'Eraser'], [10, 15, 10], color=colors)
plt.show() colors 'Pen' red incorrectly. plt.bar(['Pen', 'Notebook', 'Eraser'], [10, 15, 10])
plt.show() uses default colors without highlight.