0
0
Matplotlibdata~20 mins

Before-after comparison plots in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Before-After Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple before-after bar plot
What will be the output of this code that plots before and after values side by side?
Matplotlib
import matplotlib.pyplot as plt

before = [5, 7, 9]
after = [6, 8, 10]
labels = ['A', 'B', 'C']

x = range(len(labels))
plt.bar(x, before, width=0.4, label='Before', align='center')
plt.bar([i + 0.4 for i in x], after, width=0.4, label='After', align='center')
plt.xticks([i + 0.2 for i in x], labels)
plt.legend()
plt.show()
AA bar chart with two bars per label: 'Before' bars at positions 0,1,2 and 'After' bars shifted right by 0.4, with labels A, B, C centered between bars.
BA line plot connecting before and after values for each label.
CA scatter plot with before values on x-axis and after values on y-axis.
DA pie chart showing proportions of before and after values combined.
Attempts:
2 left
💡 Hint
Look at how the bars are positioned with the x values and the width parameter.
data_output
intermediate
1:30remaining
Number of bars in a before-after plot
Given two lists of values for before and after, how many bars will be shown in a grouped bar plot?
Matplotlib
before = [3, 4, 5, 6]
after = [4, 5, 6, 7]
# Plotting code groups bars side by side for each index
A16 bars total
B4 bars total
C2 bars total
D8 bars total
Attempts:
2 left
💡 Hint
Each index has one 'before' and one 'after' bar.
visualization
advanced
2:30remaining
Identify the correct code for a before-after line plot with markers
Which code snippet correctly creates a before-after comparison line plot with markers for each point?
A
plt.hist(before, label='Before')
plt.hist(after, label='After')
plt.legend()
plt.show()
B
plt.bar(x, before, label='Before')
plt.bar(x, after, label='After')
plt.legend()
plt.show()
C
plt.plot(x, before, marker='o', label='Before')
plt.plot(x, after, marker='o', label='After')
plt.legend()
plt.show()
D
plt.scatter(x, before, label='Before')
plt.scatter(x, after, label='After')
plt.legend()
plt.show()
Attempts:
2 left
💡 Hint
Look for line plots with markers, not bars or histograms.
🔧 Debug
advanced
2:00remaining
Identify the error in this before-after bar plot code
What error will this code produce when run?
Matplotlib
import matplotlib.pyplot as plt
before = [1, 2, 3]
after = [2, 3, 4]
labels = ['X', 'Y', 'Z']

x = range(len(labels))
plt.bar(x, before, width=0.5, label='Before')
plt.bar([i + 0.5 for i in x], after, width=0.5, label='After')
plt.xticks(x, labels)
plt.legend()
plt.show()
ATypeError: unsupported operand type(s) for +: 'range' and 'float'
BNo error, plot shows correctly
CValueError: x and height must have same first dimension
DIndexError: list index out of range
Attempts:
2 left
💡 Hint
Check the operation on the range object in plt.bar second call.
🚀 Application
expert
3:00remaining
Calculate and plot percentage change before and after
Given before and after values, which code correctly calculates the percentage change and plots it as a bar chart?
Matplotlib
import matplotlib.pyplot as plt
before = [10, 20, 30]
after = [15, 18, 33]
labels = ['P', 'Q', 'R']
A
pct_change = [(b - a) / a * 100 for a, b in zip(before, after)]
plt.bar(labels, pct_change)
plt.ylabel('Percentage Change (%)')
plt.show()
B
pct_change = [(a - b) / b * 100 for b, a in zip(before, after)]
plt.bar(labels, pct_change)
plt.ylabel('Percentage Change (%)')
plt.show()
C
pct_change = [(a / b) * 100 for b, a in zip(before, after)]
plt.bar(labels, pct_change)
plt.ylabel('Percentage Change (%)')
plt.show()
D
pct_change = [a - b for b, a in zip(before, after)]
plt.bar(labels, pct_change)
plt.ylabel('Percentage Change (%)')
plt.show()
Attempts:
2 left
💡 Hint
Percentage change formula is (new - old) / old * 100.