Before-after comparison plots help us see changes clearly by showing data from two times side by side.
Before-after comparison plots in Matplotlib
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Matplotlib
import matplotlib.pyplot as plt # Data for before and after before = [value1, value2, ...] after = [value1, value2, ...] # Create x positions x = range(len(before)) # Plot lines connecting before and after plt.plot(x, before, 'o-', label='Before') plt.plot(x, after, 's-', label='After') # Add labels and legend plt.xlabel('Items') plt.ylabel('Values') plt.title('Before-After Comparison') plt.legend() plt.show()
Use markers like 'o' and 's' to show points clearly.
Connecting lines help visualize changes between before and after.
Examples
Matplotlib
before = [5, 7, 9] after = [6, 8, 10] plt.plot(before, 'o-', label='Before') plt.plot(after, 's-', label='After') plt.legend() plt.show()
Matplotlib
x = ['A', 'B', 'C'] before = [3, 4, 5] after = [4, 5, 6] plt.plot(x, before, 'o-', label='Before') plt.plot(x, after, 's-', label='After') plt.legend() plt.show()
Matplotlib
before = [10, 15, 20] after = [12, 18, 22] x = range(len(before)) for i in x: plt.plot([i, i], [before[i], after[i]], 'k--') plt.plot(x, before, 'o-', label='Before') plt.plot(x, after, 's-', label='After') plt.legend() plt.show()
Sample Program
This code shows sales data before and after a campaign for 5 months. Dashed lines connect each month's before and after sales to highlight changes.
Matplotlib
import matplotlib.pyplot as plt # Sample data: sales before and after a campaign before = [100, 150, 200, 250, 300] after = [120, 160, 210, 280, 330] x = range(len(before)) # Plot lines connecting before and after values for i in x: plt.plot([i, i], [before[i], after[i]], 'gray', linestyle='--') plt.plot(x, before, 'o-', color='blue', label='Before') plt.plot(x, after, 's-', color='green', label='After') plt.xlabel('Month') plt.ylabel('Sales') plt.title('Sales Before and After Campaign') plt.legend() plt.show()
Important Notes
Make sure before and after lists have the same length.
Use clear labels and legends to help understand the plot.
Colors and markers make it easier to distinguish before and after data.
Summary
Before-after plots show changes clearly by plotting two sets of data points.
Connecting lines help visualize the difference for each item.
Use labels, legends, and colors to make the plot easy to read.
Practice
1. What is the main purpose of a before-after comparison plot in matplotlib?
easy
Solution
Step 1: Understand the concept of before-after plots
Before-after plots are used to compare data points from two different times or conditions to see changes.Step 2: Identify the correct purpose
Among the options, only To visually compare data from two different time points describes comparing data from two time points, which matches the before-after plot purpose.Final Answer:
To visually compare data from two different time points -> Option AQuick Check:
Before-after plots = compare two time points [OK]
Hint: Before-after plots compare two sets of data visually [OK]
Common Mistakes:
- Confusing before-after plots with distribution plots
- Thinking they show correlation instead of change
- Assuming they create 3D plots
2. Which of the following is the correct way to plot two sets of data side-by-side for before-after comparison using matplotlib?
easy
Solution
Step 1: Understand bar plot positioning
To show before and after side-by-side, bars must not overlap. Using different x positions for before and after data avoids overlap.Step 2: Analyze options for correct bar positions
plt.bar([0,1], before_data); plt.bar([1,2], after_data) places before_data at positions 0 and 1, and after_data at 1 and 2, so bars for the same category are side-by-side without overlap.Final Answer:
plt.bar([0,1], before_data); plt.bar([1,2], after_data) -> Option CQuick Check:
Side-by-side bars need different x positions [OK]
Hint: Use different x positions to avoid bar overlap [OK]
Common Mistakes:
- Plotting bars at same x positions causing overlap
- Using plt.plot instead of plt.bar for categorical data
- Passing data incorrectly as list of lists
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt before = [5, 7] after = [8, 6] plt.plot([1, 2], before, label='Before') plt.plot([1, 2], after, label='After') plt.legend() plt.show()
medium
Solution
Step 1: Understand plt.plot with x and y lists
plt.plot([1, 2], before) plots points (1,5) and (2,7) connected by a line. Similarly for after data.Step 2: Identify plot type and legend
Two line plots will appear overlapping on the same axes with labels 'Before' and 'After'. No error occurs.Final Answer:
Two overlapping line plots showing before and after data -> Option DQuick Check:
plt.plot with x,y lists = line plot [OK]
Hint: plt.plot(x, y) draws lines connecting points [OK]
Common Mistakes:
- Thinking plt.plot creates bar charts
- Expecting scatter plot without plt.scatter
- Assuming plt.plot with two lists causes error
4. Identify the error in this code for before-after bar plot:
import matplotlib.pyplot as plt before = [3, 4] after = [5, 6] plt.bar([0, 1], before) plt.bar([0, 1], after) plt.show()
medium
Solution
Step 1: Check bar positions
Both before and after bars are plotted at positions 0 and 1, causing them to overlap and hide one another.Step 2: Identify correct fix
To avoid overlap, after bars should be shifted to different x positions, e.g., [0.3, 1.3].Final Answer:
Bars for before and after overlap at same positions -> Option BQuick Check:
Same x positions cause bar overlap [OK]
Hint: Shift bars on x-axis to avoid overlap [OK]
Common Mistakes:
- Thinking plt.bar needs 3 arguments
- Ignoring bar overlap issue
- Assuming plt.show() is missing
5. You have sales data before and after a marketing campaign for 3 products: before = [100, 150, 200], after = [120, 180, 210]. How would you create a clear before-after bar plot with labels and legend in matplotlib?
hard
Solution
Step 1: Plan bar positions and labels
To compare before and after clearly, plot bars side-by-side with shifted x positions, e.g., before at [0,1,2], after at [0.3,1.3,2.3]. Add x-axis labels for products.Step 2: Add legend and labels for clarity
Use plt.legend() to distinguish before and after bars, and plt.xlabel/plt.ylabel for axis labels.Final Answer:
Use plt.bar with shifted x positions for before and after, add labels and legend -> Option AQuick Check:
Shift bars + labels + legend = clear before-after plot [OK]
Hint: Shift bars and add legend for clear comparison [OK]
Common Mistakes:
- Plotting bars at same positions causing confusion
- Skipping labels and legend
- Using scatter plot instead of bar plot
