Bird
0
0

You want to plot a bar chart with 4 bars using matplotlib, applying a colorblind-friendly palette but highlighting the third bar in orange. Which code snippet achieves this correctly?

hard📝 Application Q8 of 15
Matplotlib - Real-World Visualization Patterns
You want to plot a bar chart with 4 bars using matplotlib, applying a colorblind-friendly palette but highlighting the third bar in orange. Which code snippet achieves this correctly?
Aimport matplotlib.pyplot as plt plt.style.use('seaborn-colorblind') bars = [5, 7, 3, 4] plt.bar(range(4), bars, color='orange') plt.show()
Bimport matplotlib.pyplot as plt plt.style.use('seaborn-colorblind') colors = plt.rcParams['axes.prop_cycle'].by_key()['color'] bars = [5, 7, 3, 4] bar_colors = colors[:4] bar_colors[2] = 'orange' plt.bar(range(4), bars, color=bar_colors) plt.show()
Cimport matplotlib.pyplot as plt colors = ['blue', 'green', 'orange', 'red'] bars = [5, 7, 3, 4] plt.bar(range(4), bars, color=colors) plt.show()
Dimport matplotlib.pyplot as plt plt.style.use('seaborn-colorblind') bars = [5, 7, 3, 4] plt.bar(range(4), bars) plt.bar(2, bars[2], color='orange') plt.show()
Step-by-Step Solution
Solution:
  1. Step 1: Apply colorblind-friendly style

    Using plt.style.use('seaborn-colorblind') sets the palette.
  2. Step 2: Retrieve the palette colors

    Access colors via plt.rcParams['axes.prop_cycle'].by_key()['color'].
  3. Step 3: Modify the third bar's color

    Replace the third color with 'orange' in the color list.
  4. Step 4: Plot bars with modified colors

    Pass the updated color list to plt.bar.
  5. Final Answer:

    import matplotlib.pyplot as plt plt.style.use('seaborn-colorblind') colors = plt.rcParams['axes.prop_cycle'].by_key()['color'] bars = [5, 7, 3, 4] bar_colors = colors[:4] bar_colors[2] = 'orange' plt.bar(range(4), bars, color=bar_colors) plt.show() correctly applies the palette and highlights the third bar.
  6. Quick Check:

    Modify color list before plotting bars [OK]
Quick Trick: Modify color list to highlight bars [OK]
Common Mistakes:
  • Passing a single color string to all bars
  • Plotting bars twice causing overlap
  • Not applying the colorblind palette before plotting

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Matplotlib Quizzes