0
0
Matplotlibdata~15 mins

Multiple time series comparison in Matplotlib - Deep Dive

Choose your learning style9 modes available
Overview - Multiple time series comparison
What is it?
Multiple time series comparison means looking at two or more sets of data points collected over time to see how they relate or differ. Each time series shows how something changes, like temperature or sales, over days, months, or years. Comparing them helps find patterns, similarities, or differences. This is often done using line charts or graphs.
Why it matters
Without comparing multiple time series, we might miss important insights like which product sells better over time or how weather changes affect energy use. It helps businesses, scientists, and decision-makers understand trends and relationships. Without this, decisions would be based on guesswork, not clear evidence from data.
Where it fits
Before this, you should understand what a single time series is and how to plot it. After learning this, you can explore advanced topics like correlation analysis, forecasting multiple series together, or anomaly detection across series.
Mental Model
Core Idea
Comparing multiple time series is like watching several movies side by side to see how their stories unfold differently or similarly over time.
Think of it like...
Imagine you have several friends jogging on different paths, and you watch their speeds every minute. Comparing their speeds over time helps you see who runs faster, who slows down, or if they keep pace together.
┌─────────────────────────────┐
│ Time Series Comparison Chart │
├─────────────┬───────────────┤
│ Time (X)    │ Values (Y)    │
├─────────────┼───────────────┤
│ 1           │ Series A: 5   │
│             │ Series B: 7   │
│ 2           │ Series A: 6   │
│             │ Series B: 6   │
│ 3           │ Series A: 7   │
│             │ Series B: 8   │
└─────────────┴───────────────┘

Lines for Series A and B plotted over time to compare trends.
Build-Up - 7 Steps
1
FoundationUnderstanding single time series
🤔
Concept: Learn what a time series is and how to plot it simply.
A time series is a sequence of data points recorded at regular time intervals. For example, daily temperature readings. Using matplotlib, you can plot these points on a line chart to see how values change over time. Example code: import matplotlib.pyplot as plt time = [1, 2, 3, 4, 5] temps = [22, 21, 23, 24, 22] plt.plot(time, temps) plt.xlabel('Time (days)') plt.ylabel('Temperature (°C)') plt.title('Daily Temperature') plt.show()
Result
A simple line chart showing temperature changes over 5 days.
Understanding a single time series plot is the base for comparing multiple series later.
2
FoundationPlotting multiple lines together
🤔
Concept: Learn how to plot more than one time series on the same chart.
To compare multiple series, plot each one as a separate line on the same graph. Use different colors or styles to tell them apart. Example code: import matplotlib.pyplot as plt time = [1, 2, 3, 4, 5] series1 = [5, 6, 7, 8, 7] series2 = [3, 4, 5, 6, 5] plt.plot(time, series1, label='Series 1') plt.plot(time, series2, label='Series 2') plt.xlabel('Time') plt.ylabel('Value') plt.title('Multiple Time Series') plt.legend() plt.show()
Result
A line chart with two lines, each representing a different series over time.
Plotting multiple lines together visually reveals differences and similarities between series.
3
IntermediateUsing legends and colors effectively
🤔Before reading on: Do you think using the same color for multiple series helps or confuses the viewer? Commit to your answer.
Concept: Learn how to use colors and legends to make charts clear and easy to understand.
Assign each time series a unique color and label. The legend explains which line belongs to which series. This avoids confusion when many lines appear. Example code: plt.plot(time, series1, color='blue', label='Product A') plt.plot(time, series2, color='red', label='Product B') plt.legend() plt.show()
Result
A clear chart where each line is distinct and labeled.
Effective use of colors and legends prevents misreading and makes comparison straightforward.
4
IntermediateHandling different scales with dual axes
🤔Before reading on: Can you plot two series with very different value ranges on the same axis without confusion? Yes or no? Commit to your answer.
Concept: Learn to use two y-axes when series have very different scales to compare them clearly.
When one series has values like 10 and another like 1000, plotting on the same axis hides the smaller one. Use a second y-axis on the right side. Example code: fig, ax1 = plt.subplots() ax1.plot(time, series1, 'b-', label='Small Scale') ax1.set_ylabel('Small Scale', color='b') ax2 = ax1.twinx() ax2.plot(time, series2, 'r-', label='Large Scale') ax2.set_ylabel('Large Scale', color='r') plt.show()
Result
A chart with two y-axes, each showing one series clearly despite scale differences.
Dual axes let you compare series with different units or scales without losing detail.
5
IntermediateAligning time points and handling missing data
🤔Before reading on: Do you think time series must have data points at exactly the same times to compare? Yes or no? Commit to your answer.
Concept: Learn how to align series with different or missing time points for fair comparison.
Sometimes series have missing or irregular time points. You can fill missing data with methods like interpolation or drop unmatched points to align series. Example: import pandas as pd # Create two series with different time points s1 = pd.Series([1, 2, 3], index=[1, 2, 3]) s2 = pd.Series([2, 3, 4], index=[2, 3, 4]) # Align by reindexing and filling missing values s2_aligned = s2.reindex(s1.index).interpolate() # Now plot s1 and s2_aligned together
Result
Aligned series that can be compared point by point even if original times differ.
Handling missing or mismatched times is crucial for accurate comparison and avoids misleading conclusions.
6
AdvancedVisualizing with subplots for clarity
🤔Before reading on: Is it always better to plot all series on one chart or sometimes separate charts? Commit to your answer.
Concept: Learn to use multiple smaller charts (subplots) to compare series side by side without clutter.
When many series exist or lines overlap too much, use subplots to show each series separately but aligned by time. Example code: fig, axs = plt.subplots(2, 1, sharex=True) axs[0].plot(time, series1) axs[0].set_title('Series 1') axs[1].plot(time, series2) axs[1].set_title('Series 2') plt.show()
Result
Two stacked charts showing each series clearly with shared time axis.
Subplots reduce visual confusion and help focus on individual series trends while keeping time alignment.
7
ExpertComparing series with statistical overlays
🤔Before reading on: Do you think just plotting lines is enough to understand relationships between series? Yes or no? Commit to your answer.
Concept: Learn to add statistical measures like moving averages or confidence intervals to deepen comparison insights.
Plotting raw data can be noisy. Adding moving averages smooths trends. Confidence intervals show uncertainty. Example code: import numpy as np window = 3 moving_avg = np.convolve(series1, np.ones(window)/window, mode='valid') plt.plot(time, series1, label='Raw') plt.plot(time[window-1:], moving_avg, label='Moving Avg') plt.legend() plt.show()
Result
A chart showing both raw data and smoothed trends for clearer comparison.
Statistical overlays reveal deeper patterns and help avoid being misled by random fluctuations.
Under the Hood
Matplotlib creates plots by mapping data points to coordinates on a canvas. Each time series is drawn as a line connecting points in order of time. When multiple series are plotted, matplotlib manages layers and colors to keep them distinct. Dual axes are separate coordinate systems sharing the same x-axis but different y-axes. Internally, matplotlib uses objects like Figure and Axes to organize these elements.
Why designed this way?
Matplotlib was designed to be flexible and powerful for scientific plotting. The layered approach allows combining many plots in one figure. Dual axes solve the problem of comparing series with different units without distorting data. This design balances ease of use with customization.
┌───────────────┐
│   Figure     │
│ ┌─────────┐ │
│ │  Axes   │ │
│ │ ┌─────┐ │ │
│ │ │Line │ │ │
│ │ └─────┘ │ │
│ └─────────┘ │
└───────────────┘

Figure contains Axes; Axes contain Lines representing series.
Myth Busters - 4 Common Misconceptions
Quick: Do you think plotting multiple series on the same axis always makes comparison easier? Commit yes or no.
Common Belief:Plotting all series on the same axis is always best for comparison.
Tap to reveal reality
Reality:If series have very different scales, plotting on the same axis can hide smaller series and confuse interpretation.
Why it matters:Ignoring scale differences can lead to wrong conclusions, like thinking one series is flat when it is just small compared to another.
Quick: Do you think missing data points can be ignored safely when comparing series? Commit yes or no.
Common Belief:Missing data points don't affect comparison much and can be ignored.
Tap to reveal reality
Reality:Missing or misaligned time points can distort comparisons and hide true relationships if not handled properly.
Why it matters:Failing to align or fill missing data can cause misleading patterns or hide correlations.
Quick: Do you think adding too many series on one plot always improves insight? Commit yes or no.
Common Belief:More series on one plot always gives better insight.
Tap to reveal reality
Reality:Too many lines cause clutter and confusion, making it harder to see individual trends.
Why it matters:Overcrowded plots reduce clarity and can mislead viewers rather than inform them.
Quick: Do you think raw data lines alone are enough to understand complex series behavior? Commit yes or no.
Common Belief:Raw time series lines show all needed information clearly.
Tap to reveal reality
Reality:Raw data can be noisy; smoothing and statistical overlays often reveal clearer trends and relationships.
Why it matters:Ignoring noise can cause misinterpretation of random fluctuations as meaningful changes.
Expert Zone
1
Choosing the right time alignment method (e.g., interpolation vs. truncation) affects comparison accuracy subtly but critically.
2
Colorblind-friendly palettes and line styles improve accessibility but are often overlooked in production charts.
3
Understanding matplotlib's layering and z-order helps avoid hidden lines when plotting many series.
When NOT to use
Avoid plotting too many series on one chart; instead, use subplots or summary statistics. For very large datasets, consider dimensionality reduction or interactive visualization tools like Plotly or Bokeh.
Production Patterns
Professionals often combine multiple time series plots with interactive features like zoom and hover. They use statistical summaries and anomaly detection overlays to highlight important events. Automated reports include consistent color schemes and legends for clarity.
Connections
Correlation analysis
Builds-on
Comparing multiple time series visually is the first step before calculating numerical relationships like correlation coefficients.
Dashboard design
Same pattern
Effective multiple time series comparison principles apply directly to designing dashboards that show many metrics over time clearly.
Music composition
Opposite pattern
Just as multiple time series show simultaneous data trends, music layers different melodies over time, but the goal is harmony rather than comparison.
Common Pitfalls
#1Plotting multiple series with the same color and no legend.
Wrong approach:plt.plot(time, series1) plt.plot(time, series2) plt.show()
Correct approach:plt.plot(time, series1, label='Series 1') plt.plot(time, series2, label='Series 2') plt.legend() plt.show()
Root cause:Not labeling or coloring lines differently makes it impossible to tell which line is which.
#2Plotting series with very different scales on the same y-axis without adjustment.
Wrong approach:plt.plot(time, series1) plt.plot(time, series2) plt.show()
Correct approach:fig, ax1 = plt.subplots() ax1.plot(time, series1, 'b-') ax2 = ax1.twinx() ax2.plot(time, series2, 'r-') plt.show()
Root cause:Ignoring scale differences hides smaller series and misleads interpretation.
#3Ignoring missing time points and plotting series directly.
Wrong approach:plt.plot(time1, series1) plt.plot(time2, series2) plt.show()
Correct approach:s2_aligned = s2.reindex(s1.index).interpolate() plt.plot(s1.index, s1) plt.plot(s2_aligned.index, s2_aligned) plt.show()
Root cause:Not aligning time points causes mismatched comparisons and confusion.
Key Takeaways
Multiple time series comparison helps reveal how different data sets change over time relative to each other.
Plotting multiple lines on the same chart requires clear colors, labels, and sometimes dual axes to handle scale differences.
Aligning time points and handling missing data are essential to avoid misleading comparisons.
Using subplots or statistical overlays can improve clarity and insight beyond raw line plots.
Understanding these principles prevents common mistakes and leads to better data-driven decisions.