Colormaps help us show images with colors that make patterns easy to see. They change how numbers in images look by using colors.
Image colormaps in Matplotlib
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Matplotlib
import matplotlib.pyplot as plt plt.imshow(image_data, cmap='colormap_name') plt.colorbar() plt.show()
image_data is your image or 2D data array.
cmap sets the colormap by name, like 'gray', 'viridis', or 'hot'.
Examples
Matplotlib
plt.imshow(image_data, cmap='gray')
plt.colorbar()
plt.show()Matplotlib
plt.imshow(image_data, cmap='viridis')
plt.colorbar()
plt.show()Matplotlib
plt.imshow(image_data, cmap='hot')
plt.colorbar()
plt.show()Sample Program
This code creates a simple 10x10 gradient image and shows it using the 'viridis' colormap. The colorbar shows how colors map to values.
Matplotlib
import numpy as np import matplotlib.pyplot as plt # Create a 2D array with a gradient image_data = np.linspace(0, 1, 100).reshape(10, 10) # Show image with 'viridis' colormap plt.imshow(image_data, cmap='viridis') plt.colorbar() plt.title('Image with Viridis Colormap') plt.show()
Important Notes
Colormaps can be sequential, diverging, or qualitative depending on your data.
Use plt.colorbar() to add a color scale next to your image.
Try different colormaps to find the best one for your data story.
Summary
Colormaps change how image data values show as colors.
Use cmap in plt.imshow() to pick a colormap.
Adding a colorbar helps explain the color meaning.
Practice
1. What does the
cmap parameter do in plt.imshow() when displaying an image?easy
Solution
Step 1: Understand the role of
Thecmapinplt.imshow()cmapparameter controls the colormap, which maps numeric values to colors in the image.Step 2: Identify what
Changingcmapaffects visuallycmapchanges the colors shown, helping interpret data better.Final Answer:
It changes how numbers map to colors in the image. -> Option AQuick Check:
Colormap = color mapping [OK]
Hint: Remember: cmap controls colors, not size or titles [OK]
Common Mistakes:
- Thinking cmap changes image size
- Confusing cmap with adding titles
- Assuming cmap saves the image
2. Which of the following is the correct way to apply the 'viridis' colormap to an image using matplotlib?
easy
Solution
Step 1: Recall the correct parameter name for colormap
The parameter to set colormap inplt.imshow()iscmap, and it expects a string name.Step 2: Check the syntax for passing the colormap
Passingcmap='viridis'is correct. Usingcolororcolormapis incorrect, and omitting quotes causes an error.Final Answer:
plt.imshow(image, cmap='viridis') -> Option BQuick Check:
Use cmap='name' syntax [OK]
Hint: Use cmap='colormap_name' exactly [OK]
Common Mistakes:
- Using color instead of cmap
- Forgetting quotes around colormap name
- Using colormap instead of cmap
3. What will the following code display?
import matplotlib.pyplot as plt import numpy as np image = np.array([[0, 1], [2, 3]]) plt.imshow(image, cmap='gray') plt.colorbar() plt.show()
medium
Solution
Step 1: Understand the image data and colormap
The image is a 2x2 array with values 0 to 3. The 'gray' colormap maps low values to black and high values to white.Step 2: Analyze the colorbar and display
The colorbar shows the numeric range from 0 to 3, matching the image values. The image colors range from black (0) to white (3).Final Answer:
A 2x2 image with shades of gray from black (0) to white (3) and a colorbar showing values 0 to 3. -> Option AQuick Check:
Gray cmap maps 0-3 to black-white [OK]
Hint: Gray cmap means black to white shades [OK]
Common Mistakes:
- Thinking 'gray' is invalid
- Ignoring the colorbar range
- Assuming rainbow colors with 'gray'
4. The code below is intended to show an image with the 'hot' colormap and a colorbar, but it raises an error. What is the problem?
import matplotlib.pyplot as plt import numpy as np image = np.random.rand(3,3) plt.imshow(image, cmap=hot) plt.colorbar() plt.show()
medium
Solution
Step 1: Check the cmap parameter usage
The colormap name must be a string. Here,hotis used without quotes, causing a NameError.Step 2: Verify other code parts
The image shape is valid, and colorbar usage is correct. np.random.rand() is valid for image data.Final Answer:
The colormap name 'hot' should be a string: cmap='hot'. -> Option CQuick Check:
Colormap names need quotes [OK]
Hint: Always put colormap names in quotes [OK]
Common Mistakes:
- Forgetting quotes around colormap name
- Thinking image shape is wrong
- Misordering colorbar and imshow
5. You have a 5x5 numpy array with values from 0 to 24. You want to display it using
plt.imshow() with the 'coolwarm' colormap but only show colors for values between 5 and 20. Which code snippet correctly applies this?hard
Solution
Step 1: Understand how to limit color range in imshow
Usevminandvmaxparameters to set the data range for colormap scaling.Step 2: Check each option for correct syntax
plt.imshow(data, cmap='coolwarm', vmin=5, vmax=20); plt.colorbar() usesvmin=5andvmax=20, which is correct. Other options use invalid parameters or place them incorrectly.Final Answer:
plt.imshow(data, cmap='coolwarm', vmin=5, vmax=20); plt.colorbar() -> Option DQuick Check:
Use vmin and vmax to clip colors [OK]
Hint: Use vmin and vmax to set color limits [OK]
Common Mistakes:
- Using min/max instead of vmin/vmax
- Trying to clip with non-existent parameters
- Passing vmin/vmax to colorbar instead of imshow
