0
0
Matplotlibdata~20 mins

Bar chart color customization in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bar Chart Color Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
ABlue bars
BGreen bars
CRed bars
DBars with default color (blue)
Attempts:
2 left
💡 Hint
Look at the color argument in plt.bar function.
data_output
intermediate
2: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()
ABars colored red, blue, yellow respectively
BAll bars red
CBars colored blue, red, yellow respectively
DBars colored yellow, blue, red respectively
Attempts:
2 left
💡 Hint
The color argument accepts a list matching bars order.
visualization
advanced
3: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()
ABars with values > 5 are green, others red
BAll bars are green
CBars alternate red and green regardless of value
DBars with values > 5 are red, others green
Attempts:
2 left
💡 Hint
Look at the list comprehension for colors.
🔧 Debug
advanced
2: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()
ATypeError because color expects a list, not a string
BSyntaxError due to missing brackets
CNo error, bars colored red, blue, green
DValueError because colors list length mismatch
Attempts:
2 left
💡 Hint
Check the type of colors variable.
🚀 Application
expert
3: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()
Acolors = plt.cm.Blues(np.linspace(0.3, 1, len(values)))
Bcolors = ['lightblue', 'blue', 'darkblue', 'navy']
Ccolors = [plt.cm.Blues(i) for i in np.linspace(0.3, 1, len(values))]
Dcolors = [plt.cm.Blues(i) for i in range(len(values))]
Attempts:
2 left
💡 Hint
Use a colormap with values between 0 and 1 for gradient.