0
0
Matplotlibdata~20 mins

Horizontal grouped bars in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Horizontal Bars Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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()
AA vertical grouped bar chart with bars for each label A, B, C stacked on top of each other
BA horizontal grouped bar chart with two bars side by side for each label A, B, C, showing values from values1 and values2
CA horizontal bar chart with bars for values1 only, no grouping
DA scatter plot with points at coordinates from values1 and values2
Attempts:
2 left
💡 Hint
Look at the use of barh and the x positions shifted by width/2
data_output
intermediate
1: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()
A4
B7
C3
D12
Attempts:
2 left
💡 Hint
Count bars per group times number of groups
🔧 Debug
advanced
2: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()
ATypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'float'
BValueError: x and height must have the same size
CNo error, the chart displays correctly
DTypeError: unsupported operand type(s) for +: 'range' and 'float'
Attempts:
2 left
💡 Hint
Check the type of x and the operation x + width
visualization
advanced
1: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?
AThe first group has higher values than the second group for all months
BThe second group has higher values than the first group for all months
CBoth groups have equal values for all months
DThe chart shows stacked bars, not grouped bars
Attempts:
2 left
💡 Hint
Look at the length of bars for each group per label
🚀 Application
expert
2: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)?
Anp.arange(5) + (2 - 1.5) * 0.15
Bnp.arange(5) + (2 - 2) * 0.15
Cnp.arange(5) + (2 - 1.5) * 0.15 - 0.075
Dnp.arange(5) + 2 * 0.15
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.