Bird
Raised Fist0
Matplotlibdata~20 mins

Custom colormaps in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Custom Colormap Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Custom Colormap Creation
What is the output of this code snippet that creates a custom colormap and applies it to a simple gradient?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

colors = [(1, 0, 0), (0, 1, 0), (0, 0, 1)]  # Red, Green, Blue
custom_cmap = LinearSegmentedColormap.from_list('mycmap', colors, N=3)
data = np.linspace(0, 1, 3).reshape(1, 3)
plt.imshow(data, cmap=custom_cmap)
plt.axis('off')
plt.show()
print(custom_cmap(0.5))
A[0.0, 0.5, 0.5, 1.0]
B[1.0, 0.0, 0.0, 1.0]
C[0.0, 1.0, 0.0, 1.0]
D[0.5, 0.5, 0.0, 1.0]
Attempts:
2 left
💡 Hint
Remember that the colormap interpolates colors between the given points. The value 0.5 is halfway between red and green in the list.
❓ data_output
intermediate
1:30remaining
Number of Colors in Custom Colormap
Given this custom colormap created with N=5, how many distinct colors will it contain?
Matplotlib
from matplotlib.colors import LinearSegmentedColormap
colors = ['yellow', 'purple']
custom_cmap = LinearSegmentedColormap.from_list('custom', colors, N=5)
print(len(custom_cmap.colors) if hasattr(custom_cmap, 'colors') else 'No attribute colors')
A1
B2
C5
DNo attribute colors
Attempts:
2 left
💡 Hint
Check if the LinearSegmentedColormap object has a 'colors' attribute.
❓ visualization
advanced
2:30remaining
Visualizing a Custom Diverging Colormap
Which option produces a plot with a smooth diverging colormap from blue to white to red?
Matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

colors = ['blue', 'white', 'red']
custom_cmap = LinearSegmentedColormap.from_list('diverging', colors, N=256)
data = np.outer(np.ones(100), np.linspace(-1, 1, 100))
plt.imshow(data, cmap=custom_cmap)
plt.colorbar()
plt.show()
AA plot with a gradient from blue on the left, white in the center, and red on the right
BA plot with a gradient from red on the left, white in the center, and blue on the right
CA plot with only blue color throughout
DA plot with only red color throughout
Attempts:
2 left
💡 Hint
The order of colors in the list defines the gradient direction.
🧠 Conceptual
advanced
2:00remaining
Understanding Colormap Normalization
If you create a custom colormap with colors ['black', 'yellow'] and apply it to data ranging from 0 to 100, what happens if you normalize the data with vmin=20 and vmax=80 before plotting?
AValues below 20 map to black, values above 80 map to yellow, and values between 20 and 80 map to colors between black and yellow
BValues below 20 map to yellow, values above 80 map to black
CAll values map to colors between black and yellow regardless of their range
DNormalization has no effect on the colormap application
Attempts:
2 left
💡 Hint
Normalization rescales data to the 0-1 range before applying the colormap.
🔧 Debug
expert
2:30remaining
Identify the Error in Custom Colormap Usage
What error will this code raise when trying to create and use a custom colormap?
Matplotlib
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

colors = [(1, 0, 0), (0, 1)]  # Incorrect tuple length
custom_cmap = LinearSegmentedColormap.from_list('error_cmap', colors)
data = [[0, 1], [1, 0]]
plt.imshow(data, cmap=custom_cmap)
plt.show()
ATypeError: 'tuple' object cannot be interpreted as an integer
BValueError: color sequence must be RGB or RGBA tuples
CIndexError: tuple index out of range
DNo error, plot displays correctly
Attempts:
2 left
💡 Hint
Check the format of the color tuples in the list.

Practice

(1/5)
1. What is the main purpose of creating a custom colormap in matplotlib?
easy
A. To speed up the plotting process
B. To automatically label axes
C. To choose colors that better represent your data and improve chart clarity
D. To change the plot size

Solution

  1. Step 1: Understand what colormaps do

    Colormaps assign colors to data values in a plot to help visualize differences.
  2. Step 2: Identify the benefit of custom colormaps

    Custom colormaps let you pick colors that fit your data and style, making charts clearer and more attractive.
  3. Final Answer:

    To choose colors that better represent your data and improve chart clarity -> Option C
  4. Quick Check:

    Custom colormaps improve data visualization clarity = B [OK]
Hint: Custom colormaps improve how data colors show up [OK]
Common Mistakes:
  • Thinking colormaps speed up plotting
  • Confusing colormaps with axis labels
  • Assuming colormaps change plot size
2. Which of the following is the correct way to create a custom colormap from a list of colors in matplotlib?
easy
A. LinearSegmentedColormap.from_list('mycmap', ['red', 'green', 'blue'])
B. plt.colormap(['red', 'green', 'blue'])
C. color_map(['red', 'green', 'blue'])
D. create_colormap(['red', 'green', 'blue'])

Solution

  1. Step 1: Recall the correct function for custom colormaps

    Matplotlib uses LinearSegmentedColormap.from_list to create colormaps from color lists.
  2. Step 2: Check the syntax

    The correct syntax is LinearSegmentedColormap.from_list('mycmap', ['red', 'green', 'blue']). Other options are not valid matplotlib functions.
  3. Final Answer:

    LinearSegmentedColormap.from_list('mycmap', ['red', 'green', 'blue']) -> Option A
  4. Quick Check:

    Use LinearSegmentedColormap.from_list for custom colormaps = A [OK]
Hint: Use LinearSegmentedColormap.from_list with color list [OK]
Common Mistakes:
  • Using plt.colormap which does not exist
  • Trying to call create_colormap which is not a function
  • Confusing function names
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap

colors = ['yellow', (1, 0.5, 0), 'red']
my_cmap = LinearSegmentedColormap.from_list('mycmap', colors)
print(my_cmap(0.5))
medium
A. Error: Invalid colormap
B. (0.5, 0.5, 0.5, 1.0)
C. (1.0, 0.0, 0.0, 1.0)
D. (1.0, 0.5, 0.0, 1.0)

Solution

  1. Step 1: Understand colormap interpolation

    The colormap is created from yellow, (1, 0.5, 0), and red. At 0.5 (middle), it should be the middle anchor color (1.0, 0.5, 0.0).
  2. Step 2: Check RGBA values for the middle color

    The middle color exactly corresponds to (1.0, 0.5, 0.0, 1.0) in RGBA format, which matches the output.
  3. Final Answer:

    (1.0, 0.5, 0.0, 1.0) -> Option D
  4. Quick Check:

    Middle color in colormap = orange RGBA = A [OK]
Hint: Middle of colormap returns middle color RGBA [OK]
Common Mistakes:
  • Assuming 0.5 returns gray
  • Confusing color order
  • Expecting an error from valid code
4. Identify the error in this code that tries to create a custom colormap:
from matplotlib.colors import LinearSegmentedColormap
colors = ['blue', 'white', 'green']
my_cmap = LinearSegmentedColormap(colors)
medium
A. LinearSegmentedColormap requires 'from_list' method to create from colors
B. Colors list must be strings of hex codes only
C. You must import pyplot to create colormaps
D. The colors list cannot have more than two colors

Solution

  1. Step 1: Check how to create colormaps from colors

    LinearSegmentedColormap is a class and cannot be called directly with a list. Instead, use the class method from_list.
  2. Step 2: Identify the correct usage

    The correct way is LinearSegmentedColormap.from_list('name', colors). The code misses from_list.
  3. Final Answer:

    LinearSegmentedColormap requires 'from_list' method to create from colors -> Option A
  4. Quick Check:

    Use from_list to create colormap from colors = D [OK]
Hint: Use from_list method to create colormap from colors [OK]
Common Mistakes:
  • Calling LinearSegmentedColormap directly
  • Thinking colors must be hex codes
  • Importing pyplot unnecessarily
5. You want to create a custom colormap that smoothly transitions from blue to white to red, but with the white color occupying the middle 40% of the range. Which approach correctly achieves this?
hard
A. Use LinearSegmentedColormap.from_list('bwr', ['blue', 'white', 'red'])
B. Use LinearSegmentedColormap.from_list('bwr', ['blue', 'white', 'white', 'red'], [0, 0.3, 0.7, 1])
C. Use LinearSegmentedColormap.from_list('bwr', ['blue', 'white', 'red'], [0, 0.5, 1])
D. Use LinearSegmentedColormap.from_list('bwr', ['blue', 'red']) and set midpoint to white separately

Solution

  1. Step 1: Understand color position control in colormaps

    To make white occupy the middle 40%, you need to specify positions for colors explicitly.
  2. Step 2: Analyze the options

    Use LinearSegmentedColormap.from_list('bwr', ['blue', 'white', 'white', 'red'], [0, 0.3, 0.7, 1]) uses four colors with positions [0, 0.3, 0.7, 1], placing white twice to stretch it over 40% (from 0.3 to 0.7). This creates a smooth transition with white in the middle range.
  3. Final Answer:

    Use LinearSegmentedColormap.from_list('bwr', ['blue', 'white', 'white', 'red'], [0, 0.3, 0.7, 1]) -> Option B
  4. Quick Check:

    Specify color positions to stretch middle color = C [OK]
Hint: Repeat middle color with positions to stretch it [OK]
Common Mistakes:
  • Not specifying positions, so white is evenly spread
  • Using only three colors without position list
  • Trying to set midpoint color separately
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard