Bird
0
0

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:
  1. 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().
  2. Step 2: Use the colors list in plt.bar

    Pass the list of colors to the color parameter to color each bar differently.
  3. Final Answer:

    The code that sets the palette, retrieves the colors list, and passes it to plt.bar color parameter -> Option D
  4. 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes