Challenge - 5 Problems
Horizontal Bar Chart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of plt.barh with given data
What will be the output of this code snippet that creates a horizontal bar chart?
Matplotlib
import matplotlib.pyplot as plt categories = ['A', 'B', 'C'] values = [3, 7, 5] plt.barh(categories, values) plt.show()
Attempts:
2 left
💡 Hint
plt.barh creates horizontal bars where the first argument is the y-axis labels and the second is the bar lengths.
✗ Incorrect
plt.barh(categories, values) draws horizontal bars with categories on the y-axis and values as bar lengths. The order matches the lists.
❓ data_output
intermediate1:30remaining
Number of bars in plt.barh chart
How many bars will be displayed by this code?
Matplotlib
import matplotlib.pyplot as plt labels = ['X', 'Y', 'Z', 'W'] values = [10, 20, 15, 5] plt.barh(labels, values) plt.show()
Attempts:
2 left
💡 Hint
Count the number of labels given to plt.barh.
✗ Incorrect
There are 4 labels and 4 values, so 4 horizontal bars will be drawn.
🔧 Debug
advanced2:00remaining
Identify the error in horizontal bar chart code
What error will this code produce?
Matplotlib
import matplotlib.pyplot as plt labels = ['Jan', 'Feb', 'Mar'] values = [5, 8] plt.barh(labels, values) plt.show()
Attempts:
2 left
💡 Hint
Check if the lengths of labels and values match.
✗ Incorrect
plt.barh requires the labels and values lists to be the same length. Here, labels has 3 items but values has 2, causing a ValueError.
❓ visualization
advanced1:30remaining
Effect of changing bar height in plt.barh
What visual effect does changing the 'height' parameter in plt.barh have?
Matplotlib
import matplotlib.pyplot as plt labels = ['P', 'Q', 'R'] values = [4, 6, 8] plt.barh(labels, values, height=0.1) plt.show()
Attempts:
2 left
💡 Hint
Height controls the thickness of horizontal bars.
✗ Incorrect
In plt.barh, the height parameter controls the thickness of each horizontal bar vertically. Smaller height means thinner bars.
🚀 Application
expert3:00remaining
Create a horizontal bar chart with sorted values
Which code snippet correctly creates a horizontal bar chart with categories sorted by their values in ascending order?
Attempts:
2 left
💡 Hint
You need to sort values and categories together to keep their pairs aligned.
✗ Incorrect
Option A sorts the pairs of (value, category) so the bars appear in ascending order by value. Other options either misuse sort or break the pairing.