0
0
Matplotlibdata~30 mins

Colorbar configuration in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Colorbar configuration
📖 Scenario: You are working with a heatmap that shows temperatures across a city grid. You want to add a colorbar to explain the colors to anyone looking at the map.
🎯 Goal: Create a heatmap using matplotlib and add a colorbar with a label and custom ticks to make the map easy to understand.
📋 What You'll Learn
Create a 2D list called temperature_data with the exact values [[20, 22, 23], [21, 24, 25], [19, 20, 22]]
Create a variable called colorbar_label and set it to the string 'Temperature (°C)'
Use matplotlib.pyplot.imshow to display temperature_data as a heatmap
Add a colorbar to the heatmap with the label from colorbar_label
Set the colorbar ticks to exactly [19, 21, 23, 25]
💡 Why This Matters
🌍 Real World
Colorbars help people understand what colors mean in heatmaps, like showing temperature, population density, or elevation on maps.
💼 Career
Data scientists and analysts use colorbars to make visualizations clear and easy to interpret for reports and presentations.
Progress0 / 4 steps
1
Create the temperature data
Create a 2D list called temperature_data with these exact values: [[20, 22, 23], [21, 24, 25], [19, 20, 22]]
Matplotlib
Need a hint?

Use square brackets to create a list of lists. Each inner list is a row of temperatures.

2
Set the colorbar label
Create a variable called colorbar_label and set it to the string 'Temperature (°C)'
Matplotlib
Need a hint?

Use single or double quotes to create the string exactly as shown.

3
Create the heatmap and add a colorbar
Import matplotlib.pyplot as plt. Use plt.imshow to display temperature_data as a heatmap. Then add a colorbar using plt.colorbar() and set its label to colorbar_label.
Matplotlib
Need a hint?

Use plt.imshow to show the data and save the result to a variable. Then pass that variable to plt.colorbar(). Use set_label() on the colorbar to add the label.

4
Customize colorbar ticks and show the plot
Set the colorbar ticks to exactly [19, 21, 23, 25] using colorbar.set_ticks(). Then use plt.show() to display the heatmap with the colorbar.
Matplotlib
Need a hint?

Use colorbar.set_ticks([19, 21, 23, 25]) to set the ticks. Then call plt.show() to display the figure.