0
0
Matplotlibdata~20 mins

Colorblind-friendly palettes in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Colorblind Palette Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of color palette display code
What will be the output of this code snippet that uses a colorblind-friendly palette from matplotlib?
Matplotlib
import matplotlib.pyplot as plt
import seaborn as sns

palette = sns.color_palette("colorblind")
plt.figure(figsize=(6, 1))
plt.imshow([palette], aspect='auto')
plt.axis('off')
plt.show()
AA horizontal bar showing 8 distinct colors suitable for colorblind viewers
BA vertical bar showing 6 colors with random shades of blue
CA scatter plot with points colored in rainbow colors
DAn error because 'colorblind' is not a valid palette name in seaborn
Attempts:
2 left
💡 Hint
Think about what sns.color_palette("colorblind") returns and how imshow displays colors.
data_output
intermediate
1:30remaining
Number of colors in a colorblind-friendly palette
How many colors are in the default matplotlib 'tab10' palette, which is also considered colorblind-friendly?
Matplotlib
import matplotlib.pyplot as plt
palette = plt.get_cmap('tab10')
num_colors = palette.N
print(num_colors)
A10
B8
C12
D6
Attempts:
2 left
💡 Hint
Check the attribute that gives the number of colors in the colormap object.
visualization
advanced
2:00remaining
Identify the colorblind-friendly palette used
Given this code, which colorblind-friendly palette is being used to plot the bars?
Matplotlib
import matplotlib.pyplot as plt
import numpy as np

values = [5, 7, 3, 8, 6]
colors = plt.get_cmap('tab20c').colors[:5]
plt.bar(range(5), values, color=colors)
plt.show()
Acolorblind
Btab20c
Cviridis
DSet1
Attempts:
2 left
💡 Hint
Look at the colormap name used in plt.get_cmap.
🔧 Debug
advanced
2:00remaining
Why does this colorblind palette code raise an error?
This code raises an error. What is the cause?
Matplotlib
import matplotlib.pyplot as plt
import seaborn as sns

palette = sns.color_palette("colorblind", 10)
plt.figure(figsize=(6, 1))
plt.imshow([palette], aspect='auto')
plt.axis('off')
plt.show()
Aplt.axis('off') is invalid syntax
BThe 'colorblind' palette name is misspelled
Cimshow cannot display color palettes
DThe 'colorblind' palette only supports 8 colors, so requesting 10 causes an error
Attempts:
2 left
💡 Hint
Check the allowed number of colors for the 'colorblind' palette in seaborn.
🚀 Application
expert
2:30remaining
Choosing a colorblind-friendly palette for a scatter plot
You want to plot a scatter plot with 7 categories, ensuring colors are distinguishable for colorblind viewers. Which palette should you choose from matplotlib?
A'Pastel1' because it has soft colors but only 5 distinct colors
B'viridis' because it is a sequential palette with smooth color changes
C'tab10' because it has 10 distinct colors designed for colorblind accessibility
D'Accent' because it has only 4 colors and is not colorblind-friendly
Attempts:
2 left
💡 Hint
Consider the number of categories and colorblind accessibility of palettes.