Challenge - 5 Problems
Bar Chart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple vertical bar chart code
What will be the height of the bars in the vertical bar chart produced by this code?
Matplotlib
import matplotlib.pyplot as plt x = ['A', 'B', 'C'] y = [5, 3, 7] plt.bar(x, y) plt.show()
Attempts:
2 left
💡 Hint
Look at the y values passed to plt.bar as heights.
✗ Incorrect
The heights of the bars correspond exactly to the y list values in order: 5, 3, then 7.
❓ data_output
intermediate1:30remaining
Number of bars in the chart
How many bars will be displayed by this code snippet?
Matplotlib
import matplotlib.pyplot as plt labels = ['X', 'Y', 'Z', 'W'] values = [10, 20, 15, 5] plt.bar(labels, values) plt.show()
Attempts:
2 left
💡 Hint
Count the number of labels or values given.
✗ Incorrect
There are 4 labels and 4 values, so 4 bars will be drawn.
❓ visualization
advanced1:30remaining
Identify the bar colors in the chart
Given this code, what color will the bars be?
Matplotlib
import matplotlib.pyplot as plt categories = ['Cat1', 'Cat2'] heights = [8, 12] plt.bar(categories, heights, color='green') plt.show()
Attempts:
2 left
💡 Hint
Check the color argument in plt.bar.
✗ Incorrect
The color argument is set to 'green', so bars will be green.
🔧 Debug
advanced2:00remaining
Error type from mismatched data lengths
What error will this code raise when run?
Matplotlib
import matplotlib.pyplot as plt labels = ['A', 'B', 'C'] values = [1, 2] plt.bar(labels, values) plt.show()
Attempts:
2 left
💡 Hint
Check if the number of labels matches the number of values.
✗ Incorrect
Matplotlib requires the x and height lists to be the same length; otherwise, it raises a ValueError.
🚀 Application
expert2:30remaining
Calculate total bar height from grouped data
You have this data and code. What is the total height of all bars combined?
Matplotlib
import matplotlib.pyplot as plt fruits = ['Apple', 'Banana', 'Cherry'] counts = [4, 7, 3] plt.bar(fruits, counts) plt.show()
Attempts:
2 left
💡 Hint
Add all the values in the counts list.
✗ Incorrect
Sum of counts is 4 + 7 + 3 = 14, which is the total height of all bars combined.