📖 Scenario: You work as a data analyst and want to visualize temperature data collected over a week in different cities. A heatmap is a great way to see patterns and differences quickly.
🎯 Goal: Create a heatmap using plt.pcolormesh to visualize the temperature data for 3 cities over 7 days.
📋 What You'll Learn
Create a 2D list called temperatures with exact temperature values for 3 cities over 7 days
Create a list called days with day names from Monday to Sunday
Create a list called cities with 3 city names
Use plt.pcolormesh to plot the heatmap with correct axis labels
Add a color bar to show the temperature scale
Print the heatmap plot
💡 Why This Matters
🌍 Real World
Heatmaps are used in weather forecasting, sales analysis, and many fields to quickly spot patterns in data.
💼 Career
Data analysts and scientists use heatmaps to visualize complex data simply and communicate insights effectively.
Progress0 / 4 steps
1
Create the temperature data
Create a 2D list called temperatures with these exact values: [[22, 21, 23, 24, 25, 26, 27], [18, 19, 20, 21, 22, 23, 24], [30, 29, 28, 27, 26, 25, 24]] representing temperatures for 3 cities over 7 days.
Matplotlib
Hint
Use a list of lists to represent rows (cities) and columns (days).
2
Create the days and cities labels
Create a list called days with these exact values: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']. Also create a list called cities with these exact values: ['CityA', 'CityB', 'CityC'].
Matplotlib
Hint
Use simple lists with the exact names and values.
3
Plot the heatmap using plt.pcolormesh
Import matplotlib.pyplot as plt. Use plt.pcolormesh to plot the heatmap of temperatures. Set the x-axis ticks to days and y-axis ticks to cities. Use plt.colorbar() to add a color bar.
Matplotlib
Hint
Use shading='auto' in plt.pcolormesh for better color alignment.
4
Display the heatmap
Use plt.show() to display the heatmap plot.
Matplotlib
Hint
Use plt.show() to see the heatmap window.
Practice
(1/5)
1. What does plt.pcolormesh do in matplotlib?
easy
A. It creates a scatter plot with colored dots.
B. It plots a line graph connecting points.
C. It creates a heatmap by coloring a grid based on data values.
D. It draws bar charts with colored bars.
Solution
Step 1: Understand the function purpose
plt.pcolormesh colors a grid of cells based on the values in a 2D array, making a heatmap.
Step 2: Compare with other plot types
Line graphs connect points, scatter plots show dots, and bar charts show bars, none of which match pcolormesh.
Final Answer:
It creates a heatmap by coloring a grid based on data values. -> Option C
Quick Check:
Heatmap = pcolormesh = A [OK]
Hint: pcolormesh colors grids for heatmaps, not lines or bars [OK]
Common Mistakes:
Confusing pcolormesh with line or scatter plots
Thinking pcolormesh plots points instead of colored cells
Assuming pcolormesh creates bar charts
2. Which of these is the correct way to add a colorbar to a heatmap created with plt.pcolormesh?
easy
A. plt.colorbar()
B. plt.add_colorbar()
C. plt.show_colorbar()
D. plt.colorbar_add()
Solution
Step 1: Recall matplotlib colorbar syntax
The correct function to add a colorbar is plt.colorbar().
Step 2: Check other options
Functions like plt.add_colorbar(), plt.show_colorbar(), or plt.colorbar_add() do not exist in matplotlib.
Final Answer:
plt.colorbar() -> Option A
Quick Check:
Colorbar function = plt.colorbar() = B [OK]
Hint: Use plt.colorbar() exactly to add color legend [OK]
Common Mistakes:
Using non-existent functions like plt.add_colorbar()
Forgetting to add a colorbar after pcolormesh
Trying to add colorbar before plotting heatmap
3. What will the following code display?
import matplotlib.pyplot as plt
import numpy as np
Z = np.array([[1, 2], [3, 4]])
plt.pcolormesh(Z)
plt.colorbar()
plt.show()
medium
A. An error because Z is not a 1D array.
B. A line plot connecting points 1, 2, 3, 4.
C. A scatter plot with 4 points colored by values.
D. A 2x2 heatmap with colors representing values 1 to 4 and a colorbar.
Solution
Step 1: Understand the input array Z
Z is a 2x2 numpy array with values from 1 to 4.
Step 2: What does plt.pcolormesh(Z) do?
It creates a heatmap grid colored by the values in Z, so 4 colored squares.
Step 3: Adding plt.colorbar()
This adds a color scale legend showing how colors map to values 1 to 4.
Final Answer:
A 2x2 heatmap with colors representing values 1 to 4 and a colorbar. -> Option D
import matplotlib.pyplot as plt
import numpy as np
Z = np.array([[1, 2], [3, 4]])
plt.pcolormesh(Z)
plt.colorbar
plt.show()
medium
A. Missing parentheses after plt.colorbar causing no colorbar to show.
B. Z should be a 1D array, not 2D.
C. plt.show() must come before plt.pcolormesh().
D. plt.pcolormesh() requires x and y coordinates explicitly.
Solution
Step 1: Check plt.colorbar usage
The code uses plt.colorbar without parentheses, so the function is not called.
Step 2: Effect of missing parentheses
Without calling the function, no colorbar appears on the plot.
Final Answer:
Missing parentheses after plt.colorbar causing no colorbar to show. -> Option A
Quick Check:
Call plt.colorbar() with () to show colorbar [OK]
Hint: Always add () after plt.colorbar to display it [OK]
Common Mistakes:
Forgetting () after plt.colorbar
Thinking 2D array is invalid input
Calling plt.show() too early
Assuming x,y needed for simple heatmap
5. You want to create a heatmap of a 3x3 matrix with values from 0 to 8 using plt.pcolormesh. Which code correctly sets the x and y axis labels to 'X Axis' and 'Y Axis' and adds a colorbar?
hard
A. import matplotlib.pyplot as plt
import numpy as np
Z = np.arange(9).reshape(3,3)
plt.pcolormesh(Z)
plt.set_xlabel('X Axis')
plt.set_ylabel('Y Axis')
plt.colorbar()
plt.show()
B. import matplotlib.pyplot as plt
import numpy as np
Z = np.arange(9).reshape(3,3)
plt.pcolormesh(Z)
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.colorbar()
plt.show()
C. import matplotlib.pyplot as plt
import numpy as np
Z = np.arange(9).reshape(3,3)
plt.pcolormesh(Z)
plt.xlabel = 'X Axis'
plt.ylabel = 'Y Axis'
plt.colorbar()
plt.show()
D. import matplotlib.pyplot as plt
import numpy as np
Z = np.arange(9).reshape(3,3)
plt.pcolormesh(Z)
plt.axis_labels('X Axis', 'Y Axis')
plt.colorbar()
plt.show()
Solution
Step 1: Check axis label functions
The correct functions to label axes are plt.xlabel() and plt.ylabel().
Step 2: Verify colorbar and show usage
plt.colorbar() must be called with parentheses, and plt.show() displays the plot.
Step 3: Identify incorrect options
import matplotlib.pyplot as plt
import numpy as np
Z = np.arange(9).reshape(3,3)
plt.pcolormesh(Z)
plt.set_xlabel('X Axis')
plt.set_ylabel('Y Axis')
plt.colorbar()
plt.show() uses non-existent plt.set_xlabel(), C assigns strings instead of calling functions, D uses a non-existent plt.axis_labels().
Final Answer:
import matplotlib.pyplot as plt
import numpy as np
Z = np.arange(9).reshape(3,3)
plt.pcolormesh(Z)
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.colorbar()
plt.show() -> Option B
Quick Check:
Use plt.xlabel(), plt.ylabel(), plt.colorbar() [OK]
Hint: Use plt.xlabel() and plt.ylabel() to label axes [OK]
Common Mistakes:
Using plt.set_xlabel() instead of plt.xlabel()
Assigning labels instead of calling functions
Forgetting parentheses on plt.colorbar()
Using non-existent axis_labels() function
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.