Challenge - 5 Problems
Horizontal Bars Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of horizontal grouped bar chart code
What will be the output of this code snippet that creates a horizontal grouped bar chart using matplotlib?
Matplotlib
import matplotlib.pyplot as plt import numpy as np labels = ['A', 'B', 'C'] values1 = [5, 7, 3] values2 = [6, 2, 4] x = np.arange(len(labels)) width = 0.35 fig, ax = plt.subplots() ax.barh(x - width/2, values1, width, label='Group 1') ax.barh(x + width/2, values2, width, label='Group 2') ax.set_yticks(x) ax.set_yticklabels(labels) ax.legend() plt.show()
Attempts:
2 left
💡 Hint
Look at the use of barh and the x positions shifted by width/2
✗ Incorrect
The code uses barh to create horizontal bars. The bars for values1 and values2 are shifted left and right by width/2 to group them side by side for each label.
❓ data_output
intermediate1:30remaining
Number of bars in horizontal grouped bar chart
Given this code for a horizontal grouped bar chart, how many bars will be drawn in total?
Matplotlib
import matplotlib.pyplot as plt import numpy as np labels = ['X', 'Y', 'Z', 'W'] values1 = [3, 5, 2, 4] values2 = [1, 6, 3, 7] values3 = [2, 4, 5, 1] x = np.arange(len(labels)) width = 0.2 fig, ax = plt.subplots() ax.barh(x - width, values1, width, label='Set 1') ax.barh(x, values2, width, label='Set 2') ax.barh(x + width, values3, width, label='Set 3') ax.set_yticks(x) ax.set_yticklabels(labels) ax.legend() plt.show()
Attempts:
2 left
💡 Hint
Count bars per group times number of groups
✗ Incorrect
There are 4 labels and 3 sets of bars per label, so total bars = 4 * 3 = 12.
🔧 Debug
advanced2:00remaining
Identify the error in horizontal grouped bar chart code
What error will this code produce when trying to plot a horizontal grouped bar chart?
Matplotlib
import matplotlib.pyplot as plt import numpy as np labels = ['P', 'Q', 'R'] values1 = [4, 6, 5] values2 = [7, 3, 8] x = np.arange(len(labels)) width = 0.3 fig, ax = plt.subplots() ax.barh(x, values1, width, label='First') ax.barh(x + width, values2, width, label='Second') ax.set_yticks(x) ax.set_yticklabels(labels) ax.legend() plt.show()
Attempts:
2 left
💡 Hint
Check the type of x and the operation x + width
✗ Incorrect
x is a numpy.ndarray created by np.arange. NumPy arrays support broadcasting addition with floats, so x + width works fine and produces another ndarray. The bars abut perfectly without overlap or gap. No error occurs, and the chart displays correctly.
❓ visualization
advanced1:30remaining
Interpret the horizontal grouped bar chart visualization
You see a horizontal grouped bar chart with labels ['Jan', 'Feb', 'Mar'] on the y-axis. Each label has two bars side by side. The first bar is always longer than the second. What can you conclude about the data?
Attempts:
2 left
💡 Hint
Look at the length of bars for each group per label
✗ Incorrect
In grouped bars, side by side bars represent different groups. Longer bars mean higher values. Since the first bar is longer for all months, the first group has higher values.
🚀 Application
expert2:30remaining
Calculate bar positions for horizontal grouped bar chart
You want to plot a horizontal grouped bar chart with 4 groups and 5 bars in each group. The bar height should be 0.15. What is the correct numpy expression to calculate the y positions for the bars in the third group (index 2)?
Attempts:
2 left
💡 Hint
The offset for group j in n=4 groups is (j - (n-1)/2 ) * height = (j - 1.5) * 0.15. The y in barh is the center position.
✗ Incorrect
In horizontal grouped bar charts, y positions are the centers of the bars. For n_groups=4 and height=0.15, the centers are offset by (j - 1.5) * 0.15 to abut perfectly and center the group around the tick at np.arange(5). For j=2 (third group), offset = (2 - 1.5)*0.15 = 0.075.