0
0
Matplotlibdata~30 mins

Custom colormaps in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Custom colormaps
📖 Scenario: You are working with a heatmap that shows temperature data for a week. You want to make the colors more meaningful by creating your own color scale.
🎯 Goal: Create a custom colormap using matplotlib and apply it to a heatmap of temperature data.
📋 What You'll Learn
Create a 2D list called temperatures with exact values for 7 days and 3 times per day
Create a custom colormap named my_cmap using LinearSegmentedColormap.from_list with specified colors
Use plt.imshow with my_cmap to display the heatmap
Print the heatmap with plt.colorbar() to show the color scale
💡 Why This Matters
🌍 Real World
Custom colormaps help make data visualizations clearer and more meaningful, especially when showing temperature, elevation, or other continuous data.
💼 Career
Data scientists and analysts often create custom colormaps to highlight important data ranges and improve reports or dashboards.
Progress0 / 4 steps
1
Create the temperature data
Create a 2D list called temperatures with these exact values: [[22, 24, 19], [21, 23, 20], [20, 22, 18], [19, 21, 17], [18, 20, 16], [17, 19, 15], [16, 18, 14]]
Matplotlib
Need a hint?

Use square brackets to create a list of lists with the exact numbers.

2
Create the custom colormap
Import LinearSegmentedColormap from matplotlib.colors and create a colormap called my_cmap using LinearSegmentedColormap.from_list with the colors ['blue', 'white', 'red'].
Matplotlib
Need a hint?

Use from matplotlib.colors import LinearSegmentedColormap and then call LinearSegmentedColormap.from_list with the name and color list.

3
Plot the heatmap with the custom colormap
Import matplotlib.pyplot as plt. Use plt.imshow to display temperatures with the colormap my_cmap. Then add a colorbar with plt.colorbar().
Matplotlib
Need a hint?

Use plt.imshow with the cmap argument set to my_cmap. Then call plt.colorbar() to show the scale.

4
Show the heatmap
Add plt.show() to display the heatmap with the custom colormap and colorbar.
Matplotlib
Need a hint?

Use plt.show() to open the window with the heatmap.