0
0
Matplotlibdata~15 mins

Before-after comparison plots in Matplotlib - Deep Dive

Choose your learning style9 modes available
Overview - Before-after comparison plots
What is it?
Before-after comparison plots show how data changes from one state to another. They help us see the effect of an event, treatment, or change by displaying data points before and after it happened. These plots make it easy to spot improvements, declines, or patterns. They often use lines, bars, or dots to connect the before and after values for each item.
Why it matters
Without before-after comparison plots, it is hard to understand the impact of changes clearly. For example, if a company wants to know if a new training improved employee performance, just looking at numbers separately is confusing. These plots visually highlight differences, making decisions and explanations easier and more trustworthy. They help avoid guessing and support data-driven actions.
Where it fits
Learners should know basic plotting with matplotlib and how to handle simple datasets before this. After mastering before-after plots, they can explore more complex time series analysis, causal inference, or interactive visualizations. This topic fits in the middle of learning data visualization and analysis.
Mental Model
Core Idea
A before-after comparison plot connects paired data points from two states to clearly show changes for each item.
Think of it like...
It's like looking at photos of a garden before and after planting flowers, side by side, to see which spots got brighter and which stayed the same.
Before-After Plot Structure:

Item 1: ●────●
Item 2: ●────●
Item 3: ●────●

Each ● is a data point (before on left, after on right), connected by a line showing change.
Build-Up - 7 Steps
1
FoundationUnderstanding paired data points
🤔
Concept: Before-after plots rely on pairs of data points representing the same item at two times or conditions.
Imagine you measure the height of plants before and after watering. Each plant has two numbers: height before and height after. These pairs are the base for comparison plots.
Result
You have a list of pairs like [(5,7), (6,8), (4,5)] representing before and after values.
Understanding that each item has two linked values is key to making meaningful comparisons.
2
FoundationBasic matplotlib plotting skills
🤔
Concept: You need to know how to plot points and lines using matplotlib to create before-after plots.
Learn to plot points with plt.scatter() and lines with plt.plot(). For example, plt.scatter([1,2], [5,6]) plots points at (1,5) and (2,6).
Result
You can draw simple points and connect them with lines on a graph.
Mastering basic plotting commands lets you build more complex visualizations like before-after plots.
3
IntermediatePlotting before and after points side by side
🤔Before reading on: do you think plotting before and after points on the same x-axis or different x-positions is better for clarity? Commit to your answer.
Concept: Place before and after points for each item at different x positions to avoid overlap and clearly show change.
Assign x=1 for all 'before' points and x=2 for all 'after' points. Plot points for each item at these positions, then connect them with lines.
Result
A plot with two vertical columns of points connected by lines, showing change per item.
Separating before and after on the x-axis makes differences visually clear and easy to compare.
4
IntermediateAdding lines to connect paired points
🤔Before reading on: do you think connecting points with lines helps or confuses the comparison? Commit to your answer.
Concept: Lines between before and after points highlight the direction and size of change for each item.
Use plt.plot() to draw a line between each pair of points. For example, plt.plot([1,2], [before_value, after_value]) for each item.
Result
Lines connecting points show increases as upward slopes and decreases as downward slopes.
Connecting points with lines visually encodes the change direction and magnitude, making patterns obvious.
5
IntermediateCustomizing plots for readability
🤔
Concept: Improve clarity by adding labels, colors, and adjusting axes to make the plot easy to understand.
Add x-axis labels 'Before' and 'After', y-axis label for the measured value, and use colors to highlight positive or negative changes.
Result
A clear, labeled plot that communicates the before-after comparison effectively.
Good design choices reduce confusion and help viewers quickly grasp the story the data tells.
6
AdvancedHandling multiple groups in before-after plots
🤔Before reading on: do you think plotting all groups together or separately is better for comparison? Commit to your answer.
Concept: When data has groups (e.g., treatment vs control), plot them with different colors or subplots to compare changes within and between groups.
Use color coding or facets to separate groups. For example, plot treatment group lines in blue and control group in red.
Result
A plot that shows how different groups change, making group effects visible.
Distinguishing groups visually helps identify if changes are consistent or vary by group.
7
ExpertAvoiding misleading before-after plots
🤔Before reading on: do you think connecting points always shows true change? Commit to your answer.
Concept: Lines can mislead if data points are not paired correctly or if scale exaggerates changes; careful data handling and axis scaling are crucial.
Check data pairing carefully. Use consistent scales and consider adding summary statistics. Avoid connecting unrelated points or using distorted axes.
Result
Plots that accurately represent changes without exaggeration or confusion.
Knowing pitfalls prevents misinterpretation and maintains trust in data visualizations.
Under the Hood
Before-after plots work by mapping each item’s two values to fixed x positions (e.g., 1 and 2) and plotting their y values. Lines connect these points, visually encoding the difference. Internally, matplotlib draws points and lines on a coordinate grid, handling layering and rendering. The pairing ensures each line corresponds to one item’s change, preserving data relationships.
Why designed this way?
This design evolved to make paired comparisons intuitive and immediate. Alternatives like separate plots or tables hide individual changes. Connecting points with lines leverages human visual perception to detect trends and outliers quickly. The fixed x positions separate states clearly, avoiding clutter.
Before-After Plot Internals:

┌─────────────┐
│ Data pairs  │
│ (before, after) │
└──────┬──────┘
       │
       ▼
┌─────────────┐
│ Map to x=1 & 2 │
│ Plot points    │
└──────┬──────┘
       │
       ▼
┌─────────────┐
│ Draw lines   │
│ connecting   │
│ paired points│
└──────┬──────┘
       │
       ▼
┌─────────────┐
│ Render plot  │
│ with labels  │
└─────────────┘
Myth Busters - 3 Common Misconceptions
Quick: Do you think connecting points in before-after plots always means the change is caused by the event? Commit yes or no.
Common Belief:Connecting before and after points proves the event caused the change.
Tap to reveal reality
Reality:The plot shows change but does not prove causation; other factors might explain differences.
Why it matters:Assuming causation can lead to wrong decisions, like investing in ineffective treatments.
Quick: Do you think before-after plots can be used with unpaired data? Commit yes or no.
Common Belief:You can connect any before and after points even if they are not from the same item.
Tap to reveal reality
Reality:Lines must connect paired data from the same item; unpaired connections mislead and confuse.
Why it matters:Misconnecting points breaks the meaning of the plot and can hide true patterns.
Quick: Do you think changing the y-axis scale arbitrarily is harmless in before-after plots? Commit yes or no.
Common Belief:Adjusting y-axis scale to zoom in or out does not affect interpretation.
Tap to reveal reality
Reality:Changing scale can exaggerate or hide changes, misleading viewers about the size of effects.
Why it matters:Misleading scales reduce trust and can cause wrong conclusions.
Expert Zone
1
Small changes in axis limits can drastically alter perceived effect sizes; experts always check scales carefully.
2
When multiple groups exist, overlaying before-after lines can cause clutter; using facets or interactive plots improves clarity.
3
In noisy data, connecting all points may hide variability; summarizing with confidence intervals or smoothing can help.
When NOT to use
Before-after plots are not suitable when data points are not naturally paired or when changes happen over many time points. Alternatives like time series plots, boxplots, or difference histograms may be better.
Production Patterns
Professionals use before-after plots in clinical trials to show patient responses, in marketing to display campaign effects, and in manufacturing to compare quality before and after process changes. They often combine these plots with statistical tests and interactive dashboards.
Connections
Paired t-test
Builds-on
Understanding before-after plots helps grasp paired t-tests, which statistically test if the average change is significant.
Time series analysis
Related but broader
Before-after plots are a simple form of time series visualization focusing on two points; time series analysis extends this to many time points.
Psychology experiments
Same pattern of paired comparison
Psychology often uses before-after designs to measure treatment effects, showing how data science visualization supports experimental science.
Common Pitfalls
#1Connecting points that are not from the same item.
Wrong approach:plt.plot([1,2], [5,7]) # for item A plt.plot([1,2], [6,8]) # for item B plt.plot([1,2], [7,5]) # but lines connect wrong pairs mixing items
Correct approach:For each item, connect its own before and after values: plt.plot([1,2], [5,7]) # item A plt.plot([1,2], [6,8]) # item B plt.plot([1,2], [4,5]) # item C
Root cause:Misunderstanding that lines must connect paired data points from the same item.
#2Using the same x position for before and after points.
Wrong approach:plt.scatter([1,1,1], [5,6,7]) # before plt.scatter([1,1,1], [7,8,9]) # after plt.plot([1,1], [5,7]) # lines overlap, confusing
Correct approach:Use different x positions: plt.scatter([1,1,1], [5,6,7]) # before plt.scatter([2,2,2], [7,8,9]) # after plt.plot([1,2], [5,7]) # lines clear
Root cause:Not separating before and after points horizontally causes overlap and confusion.
#3Setting y-axis limits too narrow to exaggerate changes.
Wrong approach:plt.ylim(6,7) # zoomed in, small changes look huge
Correct approach:plt.ylim(min_value, max_value) # full range to show true scale
Root cause:Trying to make changes look bigger by manipulating axis scale misleads viewers.
Key Takeaways
Before-after comparison plots visualize paired data points to clearly show changes between two states.
Separating before and after points on the x-axis and connecting them with lines makes differences easy to see.
Careful pairing and consistent axis scaling are essential to avoid misleading interpretations.
These plots help communicate effects of changes in many fields, supporting better decisions.
Understanding their limits and proper use prevents common visualization mistakes.