0
0
Matplotlibdata~30 mins

Heatmap with plt.imshow in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Heatmap with plt.imshow
📖 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
Need a 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
Need a 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
Need a hint?

Remember to import matplotlib.pyplot as plt before using it.

4
Display the heatmap
Use plt.show() to display the heatmap.
Matplotlib
Need a hint?

Use plt.show() to see the heatmap window.