Challenge - 5 Problems
Bar Chart Color Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Bar chart with single color
What color will the bars be in this matplotlib bar chart code?
Matplotlib
import matplotlib.pyplot as plt values = [3, 7, 5] labels = ['A', 'B', 'C'] plt.bar(labels, values, color='green') plt.show()
Attempts:
2 left
💡 Hint
Look at the color argument in plt.bar function.
✗ Incorrect
The color parameter is set to 'green', so all bars will be green.
❓ data_output
intermediate2:00remaining
Bar chart with list of colors
What colors will the bars have in this matplotlib code?
Matplotlib
import matplotlib.pyplot as plt values = [4, 6, 8] labels = ['X', 'Y', 'Z'] colors = ['red', 'blue', 'yellow'] plt.bar(labels, values, color=colors) plt.show()
Attempts:
2 left
💡 Hint
The color argument accepts a list matching bars order.
✗ Incorrect
Each bar gets the color from the list in order: first red, then blue, then yellow.
❓ visualization
advanced3:00remaining
Custom color for bars based on value
Which option produces a bar chart where bars with values above 5 are red, others are green?
Matplotlib
import matplotlib.pyplot as plt values = [2, 7, 4, 9] labels = ['P', 'Q', 'R', 'S'] colors = ['red' if v > 5 else 'green' for v in values] plt.bar(labels, values, color=colors) plt.show()
Attempts:
2 left
💡 Hint
Look at the list comprehension for colors.
✗ Incorrect
The list comprehension sets 'red' for values above 5, else 'green'.
🔧 Debug
advanced2:00remaining
Identify error in bar color assignment
What error occurs when running this code?
Matplotlib
import matplotlib.pyplot as plt values = [1, 2, 3] labels = ['a', 'b', 'c'] colors = 'red, blue, green' plt.bar(labels, values, color=colors) plt.show()
Attempts:
2 left
💡 Hint
Check the type of colors variable.
✗ Incorrect
colors is a single string, not a list, so matplotlib cannot assign colors per bar.
🚀 Application
expert3:00remaining
Create a bar chart with gradient colors
Which code snippet correctly creates a bar chart with bars colored from light blue to dark blue using matplotlib?
Matplotlib
import matplotlib.pyplot as plt import numpy as np values = [5, 10, 15, 20] labels = ['W', 'X', 'Y', 'Z'] # Fill in the color assignment colors = [plt.cm.Blues(i) for i in np.linspace(0.3, 1, len(values))] plt.bar(labels, values, color=colors) plt.show()
Attempts:
2 left
💡 Hint
Use a colormap with values between 0 and 1 for gradient.
✗ Incorrect
Option C uses np.linspace to generate evenly spaced values between 0.3 and 1, then maps them to the Blues colormap, producing a gradient.