📖 Scenario: You work in a small bakery that tracks daily sales of different types of bread. You want to visualize the sales data to see which bread types sell best on which days.
🎯 Goal: Create a heatmap using plt.imshow to visualize the sales of bread types over a week.
📋 What You'll Learn
Create a 2D list called sales with exact sales numbers for 3 bread types over 4 days.
Create a list called days with the names of the 4 days.
Create a list called bread_types with the names of the 3 bread types.
Use plt.imshow to create a heatmap of the sales data.
Add x-axis labels using days and y-axis labels using bread_types.
Add a color bar to show the scale of sales.
💡 Why This Matters
🌍 Real World
Heatmaps are used in many fields like sales analysis, weather data, and biology to quickly see patterns and differences in data.
💼 Career
Knowing how to create heatmaps helps data analysts and scientists communicate complex data visually to teams and decision makers.
Progress0 / 4 steps
1
Create the sales data
Create a 2D list called sales with these exact values: [[10, 15, 7, 12], [5, 8, 6, 9], [12, 18, 14, 20]] representing sales of 3 bread types over 4 days.
Matplotlib
Hint
Think of sales as a table with 3 rows and 4 columns.
2
Create labels for days and bread types
Create a list called days with these exact values: ["Monday", "Tuesday", "Wednesday", "Thursday"] and a list called bread_types with these exact values: ["Sourdough", "Baguette", "Rye"].
Matplotlib
Hint
Use simple list syntax with exact strings for days and bread types.
3
Create the heatmap with plt.imshow
Import matplotlib.pyplot as plt. Use plt.imshow with the sales data and cmap='YlGn' to create a heatmap. Set x-axis ticks to the range of days and y-axis ticks to the range of bread types. Use plt.xticks with range(len(days)) and days, and plt.yticks with range(len(bread_types)) and bread_types. Add a color bar with plt.colorbar().
Matplotlib
Hint
Remember to import matplotlib.pyplot as plt before using it.
4
Display the heatmap
Use plt.show() to display the heatmap.
Matplotlib
Hint
Use plt.show() to see the heatmap window.
Practice
(1/5)
1. What does the plt.imshow function do in matplotlib when given a 2D array?
easy
A. It prints the array values as text on the plot.
B. It plots a line graph of the array values.
C. It creates a heatmap by showing the array values as colors.
D. It creates a pie chart from the array values.
Solution
Step 1: Understand plt.imshow input
The function takes a 2D array and maps its values to colors in a grid.
Step 2: Recognize heatmap behavior
This color mapping creates a heatmap, showing patterns visually.
Final Answer:
It creates a heatmap by showing the array values as colors. -> Option C
Quick Check:
plt.imshow = heatmap colors [OK]
Hint: plt.imshow shows 2D data as colored grids [OK]
Common Mistakes:
Thinking plt.imshow plots lines or text
Confusing heatmap with pie chart
Assuming plt.imshow prints numbers
2. Which of these is the correct way to add a color bar to a heatmap created with plt.imshow?
easy
A. plt.colorbar()
B. plt.addcolor()
C. plt.showcolor()
D. plt.colorheat()
Solution
Step 1: Recall color bar function
Matplotlib uses plt.colorbar() to add a color scale next to images.
Step 2: Check other options
Other options are not valid matplotlib functions.
Final Answer:
plt.colorbar() -> Option A
Quick Check:
Color bar function = plt.colorbar() [OK]
Hint: Use plt.colorbar() to add color scale [OK]
Common Mistakes:
Using non-existent functions like plt.addcolor()
Forgetting to call plt.colorbar() after plt.imshow()
Trying to add color bar before plotting heatmap
3. What will the following code display?
import matplotlib.pyplot as plt
import numpy as np
data = np.array([[1, 2], [3, 4]])
plt.imshow(data, cmap='hot')
plt.colorbar()
plt.show()
medium
A. A scatter plot of points (1,2) and (3,4) with hot colors.
B. A line plot of the numbers 1, 2, 3, 4 with a color bar.
C. An error because cmap='hot' is invalid.
D. A 2x2 heatmap with colors from black to red to yellow, and a color bar showing values 1 to 4.
Solution
Step 1: Understand plt.imshow with cmap='hot'
The data array is 2x2, so plt.imshow creates a 2x2 grid colored by values using the 'hot' color map (black to red to yellow).
Step 2: Effect of plt.colorbar()
The color bar shows the scale from the minimum value 1 to maximum 4, helping interpret colors.
Final Answer:
A 2x2 heatmap with colors from black to red to yellow, and a color bar showing values 1 to 4. -> Option D
Quick Check:
plt.imshow + cmap='hot' + colorbar = heatmap with hot colors [OK]
Hint: cmap='hot' colors low to high values black-red-yellow [OK]
Common Mistakes:
Thinking plt.imshow plots lines or scatter
Believing cmap='hot' causes error
Ignoring color bar meaning
4. The code below is intended to show a heatmap but does not display the color bar. What is the error?
import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(3,3)
plt.imshow(data)
plt.colorbar
plt.show()
medium
A. plt.colorbar is missing parentheses, so color bar is not shown.
B. plt.imshow needs a cmap argument to show color bar.
C. plt.show() must come before plt.colorbar().
D. np.random.rand(3,3) creates invalid data for heatmap.
Solution
Step 1: Identify plt.colorbar usage
The code uses plt.colorbar without parentheses, so the function is not called.
Step 2: Correct function call
Adding parentheses plt.colorbar() calls the function and displays the color bar.
Final Answer:
plt.colorbar is missing parentheses, so color bar is not shown. -> Option A
Quick Check:
Call plt.colorbar() with () to show color bar [OK]
Hint: Always call plt.colorbar() with parentheses [OK]
Common Mistakes:
Forgetting parentheses on plt.colorbar
Thinking cmap is required for color bar
Calling plt.show() before plt.colorbar
5. You have a 4x4 matrix with values from 0 to 15. You want to create a heatmap using plt.imshow that shows the values clearly with a color bar, but you want the color scale to always be from 0 to 20 regardless of data. Which code snippet correctly does this?
hard
A. plt.imshow(matrix, vmin=0, vmax=15)
plt.colorbar()
plt.show()
B. plt.imshow(matrix, vmin=0, vmax=20)
plt.colorbar()
plt.show()
C. plt.imshow(matrix, cmap='gray')
plt.colorbar()
plt.show()
D. plt.imshow(matrix)
plt.colorbar(vmin=0, vmax=20)
plt.show()
Solution
Step 1: Set color scale limits in plt.imshow
Use vmin=0 and vmax=20 in plt.imshow to fix color scale from 0 to 20.
Step 2: Add color bar normally
Call plt.colorbar() to show the color scale matching the fixed limits.
Final Answer:
plt.imshow(matrix, vmin=0, vmax=20)
plt.colorbar()
plt.show() -> Option B
Quick Check:
Set vmin/vmax in imshow, then add colorbar [OK]
Hint: Use vmin and vmax in plt.imshow to fix color scale [OK]
Common Mistakes:
Setting vmin/vmax in plt.colorbar instead of plt.imshow
Not setting vmax to 20, so scale auto adjusts
Using wrong vmin/vmax values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.