0
0
Matplotlibdata~30 mins

Color mapping with colorbar in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Color mapping with colorbar
📖 Scenario: You are working with temperature data for a week. You want to visualize the temperatures using colors so it is easy to see which days were hotter or colder.
🎯 Goal: Create a color map for the temperature values and display a colorbar to show the temperature scale.
📋 What You'll Learn
Create a list of temperatures for 7 days
Create a colormap using matplotlib
Map the temperatures to colors using the colormap
Display a colorbar showing the temperature scale
💡 Why This Matters
🌍 Real World
Color mapping with colorbars is used in weather maps, heatmaps, and scientific visualizations to show data intensity or value ranges clearly.
💼 Career
Data scientists and analysts use color mapping to make data easier to understand visually, helping in reports and presentations.
Progress0 / 4 steps
1
Create the temperature data list
Create a list called temperatures with these exact values: 22, 25, 19, 30, 28, 24, 20 representing temperatures for 7 days.
Matplotlib
Need a hint?

Use square brackets to create a list and separate values with commas.

2
Create a colormap and normalization
Import matplotlib.pyplot as plt and Normalize from matplotlib.colors. Then create a Normalize object called norm with vmin=19 and vmax=30 to normalize the temperature range.
Matplotlib
Need a hint?

Use import matplotlib.pyplot as plt and from matplotlib.colors import Normalize.

Create norm = Normalize(vmin=19, vmax=30).

3
Map temperatures to colors using a colormap
Create a variable called cmap and set it to plt.cm.viridis. Then create a list called colors by applying cmap(norm(temp)) for each temp in temperatures using a list comprehension.
Matplotlib
Need a hint?

Use plt.cm.viridis for the colormap.

Use a list comprehension to map each temperature to a color.

4
Display the colorbar with the temperature scale
Create a figure and axis using plt.subplots(). Then create a ScalarMappable object called sm with the norm and cmap. Use sm.set_array([]). Finally, add a colorbar to the axis with plt.colorbar(sm, ax=ax) and call plt.show() to display the plot.
Matplotlib
Need a hint?

Use ScalarMappable to create a mappable for the colorbar.

Call plt.colorbar with the mappable and axis.