📖 Scenario: You are working with a heatmap to visualize temperature data across a city grid. You want to add a colorbar to your heatmap to help viewers understand the temperature scale.
🎯 Goal: Create a heatmap using matplotlib and add a colorbar with customized formatting to clearly show the temperature scale.
📋 What You'll Learn
Create a 2D list called temperature_data with exact values
Create a matplotlib figure and axis
Plot the heatmap using imshow with temperature_data
Add a colorbar to the heatmap and format its label and ticks
Print the colorbar's label text to confirm formatting
💡 Why This Matters
🌍 Real World
Colorbars help people understand the meaning of colors in heatmaps or other visualizations, such as temperature maps or population density maps.
💼 Career
Data scientists and analysts often customize colorbars to make their visualizations clearer and more professional for reports and presentations.
Progress0 / 4 steps
1
Create the temperature data
Create a 2D list called temperature_data with these exact values: [[22, 24, 23], [25, 27, 26], [24, 23, 22]]
Matplotlib
Hint
Use square brackets to create a list of lists with the exact numbers.
2
Set up the plot
Import matplotlib.pyplot as plt. Create a figure and axis using plt.subplots().
Matplotlib
Hint
Use import matplotlib.pyplot as plt and then fig, ax = plt.subplots().
3
Plot heatmap and add colorbar
Use ax.imshow(temperature_data) to plot the heatmap. Then add a colorbar with fig.colorbar() and assign it to cbar. Set the colorbar label to 'Temperature (°C)' and set the label font size to 12.
Matplotlib
Hint
Use ax.imshow() to plot, then fig.colorbar() to add the colorbar. Use cbar.set_label() to set the label and font size.
4
Display the plot and print colorbar label
Print the colorbar label text using print(cbar.ax.yaxis.label.get_text()). Then display the plot with plt.show().
Matplotlib
Hint
Use print(cbar.ax.yaxis.label.get_text()) to print the label text, then plt.show() to display the plot.
Practice
(1/5)
1. What is the main purpose of adding a colorbar to a matplotlib plot?
easy
A. To show the scale of colors representing data values
B. To add a title to the plot
C. To change the background color of the plot
D. To display the axis labels
Solution
Step 1: Understand the role of color in plots
Colors in a plot often represent data values or categories.
Step 2: Purpose of colorbar
A colorbar explains what each color means by showing a scale or legend.
Final Answer:
To show the scale of colors representing data values -> Option A
Quick Check:
Colorbar = color scale legend [OK]
Hint: Colorbar explains colors' meaning in data [OK]
Common Mistakes:
Confusing colorbar with plot title
Thinking colorbar changes plot background
Mixing colorbar with axis labels
2. Which of the following is the correct way to add a colorbar to a matplotlib plot stored in variable im?
easy
A. plt.colorbar()
B. plt.colorbar(im)
C. im.colorbar()
D. plt.add_colorbar(im)
Solution
Step 1: Recall matplotlib colorbar syntax
The function plt.colorbar() requires the image or mappable object as an argument.
Step 2: Check each option
plt.colorbar(im) correctly calls plt.colorbar(im). plt.colorbar() misses the argument. im.colorbar() is invalid method. plt.add_colorbar(im) is not a matplotlib function.
Final Answer:
plt.colorbar(im) -> Option B
Quick Check:
plt.colorbar(im) adds colorbar [OK]
Hint: Always pass the image object to plt.colorbar() [OK]
Common Mistakes:
Calling plt.colorbar() without argument
Using non-existent methods like im.colorbar()
Trying plt.add_colorbar() which doesn't exist
3. What will be the effect of this code snippet on the colorbar ticks?
Hint: Pass labels as a list to set_ticklabels [OK]
Common Mistakes:
Passing multiple strings instead of a list
Calling set_ticks after set_ticklabels (order doesn't matter)
Misunderstanding plt.colorbar() return value
Thinking imshow needs integers only
5. You want to format a colorbar to show percentage ticks from 0% to 100% with steps of 25%. Which code snippet correctly formats the colorbar ticks and labels?