0
0
Matplotlibdata~20 mins

Image colormaps in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Colormap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of applying 'viridis' colormap to a grayscale image
What is the shape and data type of the output array after applying the 'viridis' colormap to a 2D grayscale image using matplotlib's cm.viridis?
Matplotlib
import numpy as np
import matplotlib.cm as cm

gray_image = np.array([[0, 128], [192, 255]], dtype=np.uint8)
colored_image = cm.viridis(gray_image / 255.0)
print(colored_image.shape, colored_image.dtype)
A(2, 2, 4) float32
B(2, 2, 4) float64
C(2, 2, 3) float32
D(2, 2) uint8
Attempts:
2 left
💡 Hint
Remember that matplotlib colormaps return RGBA values as floats between 0 and 1.
data_output
intermediate
1:30remaining
Number of unique colors after applying 'gray' colormap
Given a 1D numpy array with values [0, 0.5, 1.0], what is the number of unique colors in the output after applying the 'gray' colormap from matplotlib?
Matplotlib
import numpy as np
import matplotlib.cm as cm

values = np.array([0, 0.5, 1.0])
colors = cm.gray(values)
unique_colors = np.unique(colors, axis=0)
print(len(unique_colors))
A3
B2
C1
D4
Attempts:
2 left
💡 Hint
Check if the colormap maps different input values to distinct RGBA colors.
visualization
advanced
2:30remaining
Visualizing the effect of 'hot' colormap on a gradient image
Which option shows the correct matplotlib code to display a horizontal gradient image with the 'hot' colormap applied?
A
import numpy as np
import matplotlib.pyplot as plt
img = np.linspace(0, 1, 256).reshape(-1, 1)
plt.imshow(img, cmap='hot')
plt.axis('on')
plt.show()
B
import numpy as np
import matplotlib.pyplot as plt
img = np.linspace(0, 1, 256)
plt.imshow(img, cmap='hot')
plt.axis('off')
plt.show()
C
import numpy as np
import matplotlib.pyplot as plt
img = np.linspace(0, 1, 256).reshape(1, -1)
plt.imshow(img, cmap='hot')
plt.axis('on')
plt.show()
D
import numpy as np
import matplotlib.pyplot as plt
img = np.linspace(0, 1, 256).reshape(1, -1)
plt.imshow(img, cmap='hot', aspect='auto')
plt.axis('off')
plt.show()
Attempts:
2 left
💡 Hint
A horizontal gradient image should have shape (1, width) and axis turned off for clean display.
🔧 Debug
advanced
1:30remaining
Identify the error when applying colormap to integer image
What error will occur when running this code snippet?
import numpy as np
import matplotlib.cm as cm
img = np.array([[0, 255], [128, 64]], dtype=np.uint8)
colored = cm.plasma(img)
print(colored)
ATypeError: float argument required, not numpy.uint8
BValueError: Input values must be in the range 0 to 1
CNo error, prints RGBA array
DAttributeError: module 'matplotlib.cm' has no attribute 'plasma'
Attempts:
2 left
💡 Hint
Colormaps expect input values normalized between 0 and 1.
🚀 Application
expert
3:00remaining
Combining two colormaps to create a custom colormap
Which code correctly creates a new colormap by combining the first half of 'Blues' and the second half of 'Reds' colormaps?
A
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

blues = plt.cm.Blues(np.linspace(0, 1, 128))
reds = plt.cm.Reds(np.linspace(0, 1, 128))
newcolors = np.vstack((blues[:128], reds[128:]))
newcmp = LinearSegmentedColormap.from_list('BlueRed', newcolors)
print(newcmp(0.5))
B
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

blues = plt.cm.Blues(np.linspace(0, 1, 128))
reds = plt.cm.Reds(np.linspace(0, 1, 128))
newcolors = np.vstack((blues[:64], reds[64:]))
newcmp = LinearSegmentedColormap.from_list('BlueRed', newcolors)
print(newcmp(0.75))
C
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

blues = plt.cm.Blues(np.linspace(0, 1, 256))
reds = plt.cm.Reds(np.linspace(0, 1, 256))
newcolors = np.vstack((blues[:128], reds[128:]))
newcmp = LinearSegmentedColormap.from_list('BlueRed', newcolors)
print(newcmp(0.5))
D
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

blues = plt.cm.Blues(np.linspace(0, 1, 128))
reds = plt.cm.Reds(np.linspace(0, 1, 128))
newcolors = np.vstack((blues[:64], reds[64:]))
newcmp = LinearSegmentedColormap.from_list('BlueRed', newcolors)
print(newcmp(0.5))
Attempts:
2 left
💡 Hint
Ensure the slices cover half of the arrays and the arrays have matching sizes before stacking.