0
0
Matplotlibdata~20 mins

Grouped bar charts in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Grouped Bar Chart Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output of grouped bar chart code
What will be the height of the first bar in the first group when this code runs?
Matplotlib
import matplotlib.pyplot as plt

labels = ['A', 'B', 'C']
values1 = [5, 7, 3]
values2 = [6, 2, 4]

x = range(len(labels))
width = 0.35

fig, ax = plt.subplots()
ax.bar(x, values1, width, label='Group 1')
ax.bar([p + width for p in x], values2, width, label='Group 2')
plt.show()
A3
B6
C5
D7
Attempts:
2 left
💡 Hint
Look at the values1 list and the first bar plotted.
data_output
intermediate
1:30remaining
Number of bars in grouped bar chart
How many bars will be displayed in total when this grouped bar chart code runs?
Matplotlib
import matplotlib.pyplot as plt

labels = ['X', 'Y', 'Z', 'W']
values1 = [1, 2, 3, 4]
values2 = [4, 3, 2, 1]
values3 = [2, 3, 4, 5]

x = range(len(labels))
width = 0.2

fig, ax = plt.subplots()
ax.bar(x, values1, width, label='Set 1')
ax.bar([p + width for p in x], values2, width, label='Set 2')
ax.bar([p + 2*width for p in x], values3, width, label='Set 3')
plt.show()
A16
B12
C8
D4
Attempts:
2 left
💡 Hint
Count bars per group and multiply by number of groups.
visualization
advanced
2:00remaining
Identify the correct grouped bar chart
Which option shows the correct grouped bar chart for these data sets?
Matplotlib
import matplotlib.pyplot as plt

labels = ['Jan', 'Feb', 'Mar']
sales_A = [10, 15, 7]
sales_B = [12, 9, 14]

x = range(len(labels))
width = 0.4

fig, ax = plt.subplots()
ax.bar(x, sales_A, width, label='Product A')
ax.bar([p + width for p in x], sales_B, width, label='Product B')
plt.xticks([p + width/2 for p in x], labels)
plt.legend()
plt.show()
ABars for Product A and Product B are plotted with large gaps between each bar.
BBars for Product A and Product B overlap exactly at each label position.
CBars for Product A and Product B are stacked on top of each other.
DBars for Product A and Product B are side by side with a small gap between groups.
Attempts:
2 left
💡 Hint
Grouped bars are side by side, not stacked or overlapping.
🔧 Debug
advanced
1:30remaining
Error in grouped bar chart code
What error will this code produce when run?
Matplotlib
import matplotlib.pyplot as plt

labels = ['A', 'B']
values1 = [3, 5, 7]
values2 = [2, 4]

x = range(len(labels))
width = 0.3

fig, ax = plt.subplots()
ax.bar(x, values1, width, label='Set 1')
ax.bar([p + width for p in x], values2, width, label='Set 2')
plt.show()
AValueError: x and height must have same first dimension
BTypeError: unsupported operand type(s) for +: 'range' and 'float'
CIndexError: list index out of range
DNo error, chart displays correctly
Attempts:
2 left
💡 Hint
Check if the lengths of x and values1 match.
🚀 Application
expert
2:00remaining
Calculate total height per group from grouped bar chart data
Given these grouped bar chart data, what is the total height of bars for label 'C'?
Matplotlib
labels = ['A', 'B', 'C', 'D']
values1 = [4, 5, 6, 7]
values2 = [1, 2, 3, 4]
values3 = [7, 8, 9, 10]
A18
B22
C9
D15
Attempts:
2 left
💡 Hint
Add the values for label 'C' from all three lists.