Which code snippet correctly applies a colorblind-friendly palette and plots the bars with different colors?
hard📝 Application Q15 of 15
Matplotlib - Real-World Visualization Patterns
You want to create a bar chart with 5 bars using matplotlib and ensure it is colorblind-friendly. Which code snippet correctly applies a colorblind-friendly palette and plots the bars with different colors?
Aimport seaborn as sns
import matplotlib.pyplot as plt
plt.bar(range(5), [1,2,3,4,5])
sns.set_palette('colorblind')
plt.show()
Bimport matplotlib.pyplot as plt
plt.bar(range(5), [1,2,3,4,5], color='colorblind')
plt.show()
Cimport seaborn as sns
import matplotlib.pyplot as plt
colors = sns.set_palette('colorblind')
plt.bar(range(5), [1,2,3,4,5], color=colors)
plt.show()
Dimport seaborn as sns
import matplotlib.pyplot as plt
sns.set_palette('colorblind')
colors = sns.color_palette()
plt.bar(range(5), [1,2,3,4,5], color=colors)
plt.show()
Step-by-Step Solution
Solution:
Step 1: Apply the colorblind palette correctly
Use sns.set_palette('colorblind') to set the palette globally, then get the colors with sns.color_palette().
Step 2: Use the colors list in plt.bar
Pass the list of colors to the color parameter to color each bar differently.
Final Answer:
The code that sets the palette, retrieves the colors list, and passes it to plt.bar color parameter -> Option D
Quick Check:
Set palette + use colors list = A [OK]
Quick Trick:Set palette, get colors list, pass to color parameter [OK]
Common Mistakes:
Passing palette name as color string
Assigning sns.set_palette() return to colors
Not passing colors list to bar plot
Master "Real-World Visualization Patterns" in Matplotlib
9 interactive learning modes - each teaches the same concept differently