Challenge - 5 Problems
Before-After Plot Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Look at how the bars are positioned with the x values and the width parameter.
✗ Incorrect
The code creates two bar charts side by side for each label by shifting the 'after' bars by 0.4 on the x-axis. The xticks are set to be centered between the two bars for each label.
❓ data_output
intermediate1: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
Attempts:
2 left
💡 Hint
Each index has one 'before' and one 'after' bar.
✗ Incorrect
There are 4 indices, each with 2 bars (before and after), so total bars = 4 * 2 = 8.
❓ visualization
advanced2: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?
Attempts:
2 left
💡 Hint
Look for line plots with markers, not bars or histograms.
✗ Incorrect
Option C uses plt.plot with markers to show lines connecting before and after values, which is typical for before-after comparison line plots.
🔧 Debug
advanced2: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()
Attempts:
2 left
💡 Hint
Check the operation on the range object in plt.bar second call.
✗ Incorrect
You cannot add a float to a range object directly. You need to convert range to a list or use a list comprehension.
🚀 Application
expert3: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']
Attempts:
2 left
💡 Hint
Percentage change formula is (new - old) / old * 100.
✗ Incorrect
Option B correctly computes percentage change as (after - before) / before * 100 and plots it.