Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the module needed to create custom colormaps in matplotlib.
Matplotlib
from matplotlib import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'pyplot' instead of 'colors'.
Using 'cm' which is for colormap instances, not creation.
✗ Incorrect
The 'colors' module in matplotlib is used to create and manipulate colormaps.
2fill in blank
mediumComplete the code to create a linear segmented colormap from a list of colors.
Matplotlib
custom_cmap = colors.LinearSegmentedColormap.from_list('my_cmap', [1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string of colors instead of a list.
Using a set or tuple instead of a list.
✗ Incorrect
The from_list method expects a list of colors to create the colormap.
3fill in blank
hardFix the error in the code to correctly create a colormap with three colors.
Matplotlib
cmap = colors.LinearSegmentedColormap.from_list('cmap', ['red', [1], 'blue'])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using color names without quotes causing syntax errors.
Using invalid color names.
✗ Incorrect
Colors must be strings, so 'green' with quotes is correct.
4fill in blank
hardFill both blanks to create a colormap with three colors and use it in a scatter plot.
Matplotlib
cmap = colors.LinearSegmentedColormap.from_list('cmap', ['red', [1], 'blue']) plt.scatter(x, y, c=z, cmap=[2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the colormap name as a string instead of the variable.
Using wrong color names or missing quotes.
✗ Incorrect
The colormap uses 'green' as the middle color and the variable 'cmap' is passed to scatter.
5fill in blank
hardFill all three blanks to create a reversed colormap and apply it to an image plot.
Matplotlib
base_cmap = colors.LinearSegmentedColormap.from_list('base', ['blue', [1], 'red']) reversed_cmap = base_cmap.[2]() plt.imshow(data, cmap=[3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'reverse' instead of 'reversed()' method.
Passing the colormap name as a string instead of the variable.
✗ Incorrect
The middle color is 'white', the method to reverse a colormap is 'reversed()', and the reversed colormap variable is passed to imshow.