0
0
Matplotlibdata~30 mins

Image colormaps in Matplotlib - Mini Project: Build & Apply

Choose your learning style9 modes available
Image colormaps
📖 Scenario: You are working with a grayscale image represented as a 2D array of pixel brightness values. You want to visualize this image using different color styles called colormaps to better understand the details.
🎯 Goal: Create a small 2D array representing a grayscale image, set a colormap name, apply the colormap to the image using matplotlib, and display the colored image.
📋 What You'll Learn
Create a 2D numpy array called image with the exact values [[0, 50, 100], [150, 200, 255]]
Create a variable called cmap_name and set it to the string 'viridis'
Use matplotlib's imshow function with the cmap parameter set to cmap_name to display the image
Use plt.colorbar() to show the color scale next to the image
Use plt.show() to display the final image
💡 Why This Matters
🌍 Real World
Scientists and engineers often use colormaps to visualize grayscale images such as medical scans, satellite photos, or heatmaps to better understand patterns and details.
💼 Career
Knowing how to apply colormaps is useful for data scientists and analysts who need to present image data clearly and effectively in reports and dashboards.
Progress0 / 4 steps
1
Create the grayscale image array
Create a 2D numpy array called image with these exact values: [[0, 50, 100], [150, 200, 255]]
Matplotlib
Need a hint?

Use np.array and pass the list of lists with the exact numbers.

2
Set the colormap name
Create a variable called cmap_name and set it to the string 'viridis'
Matplotlib
Need a hint?

Assign the string 'viridis' to the variable cmap_name.

3
Display the image with the colormap
Use plt.imshow with the image array and set the cmap parameter to cmap_name to display the colored image
Matplotlib
Need a hint?

Call plt.imshow with image and cmap=cmap_name.

4
Show the colorbar and display the image
Add plt.colorbar() to show the color scale and then call plt.show() to display the image
Matplotlib
Need a hint?

Call plt.colorbar() before plt.show() to add the color scale.