0
0
Matplotlibdata~10 mins

Multiple time series comparison in Matplotlib - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple time series comparison
Load data for series A
Load data for series B
Prepare time axis
Plot series A on graph
Plot series B on graph
Add labels and legend
Show combined plot
END
Load multiple time series data, plot them on the same graph with labels and legend, then display for comparison.
Execution Sample
Matplotlib
import matplotlib.pyplot as plt
import pandas as pd

# Create two time series data
series1 = pd.Series([1, 3, 2, 5, 7], index=pd.date_range('2024-01-01', periods=5))
series2 = pd.Series([2, 4, 1, 6, 8], index=pd.date_range('2024-01-01', periods=5))

plt.plot(series1.index, series1, label='Series 1')
plt.plot(series2.index, series2, label='Series 2')
plt.legend()
plt.show()
This code plots two time series on the same graph to compare their values over the same dates.
Execution Table
StepActionData/VariablesResult/Output
1Create series1series1 = [1,3,2,5,7] with dates 2024-01-01 to 2024-01-05series1 ready with 5 data points
2Create series2series2 = [2,4,1,6,8] with same datesseries2 ready with 5 data points
3Plot series1x=dates, y=series1 valuesLine for series1 drawn on plot
4Plot series2x=dates, y=series2 valuesLine for series2 drawn on plot
5Add legendlabels: Series 1, Series 2Legend displayed on plot
6Show plotCombined plot with both seriesGraph window opens showing both lines
7EndAll plotting doneExecution stops
💡 All time series plotted and displayed, program ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 6
series1None[1,3,2,5,7] with dates[1,3,2,5,7] with dates[1,3,2,5,7] with dates
series2NoneNone[2,4,1,6,8] with dates[2,4,1,6,8] with dates
plotEmptyEmptyEmptyDisplayed plot with both lines
Key Moments - 3 Insights
Why do both series use the same date range as the x-axis?
Both series are created with the same date index, so when plotting, the x-axis aligns perfectly for comparison, as shown in execution_table steps 1 and 2.
What happens if we plot series without labels?
Without labels, the legend would be empty or missing, making it hard to tell which line is which. Step 5 shows adding labels to fix this.
Why do we call plt.show() at the end?
plt.show() displays the plot window with all lines drawn. Without it, the plot may not appear. This is shown in step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is plotted on the x-axis?
ADates from 2024-01-01 to 2024-01-05
BValues of series1
CValues of series2
DLegend labels
💡 Hint
Check the 'Data/Variables' column at step 3 in execution_table
At which step are both series visible on the plot?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look for when series2 is plotted in execution_table
If we remove plt.legend(), what changes in the output?
ANo lines will be drawn
BPlot will show but no legend labels
CPlot will not show at all
DSeries data will be lost
💡 Hint
Refer to step 5 in execution_table about adding legend
Concept Snapshot
Multiple time series comparison:
- Plot multiple series on same graph using plt.plot()
- Use same x-axis (time) for alignment
- Add labels and plt.legend() for clarity
- Call plt.show() to display
- Helps visually compare trends and values
Full Transcript
This visual execution shows how to compare multiple time series using matplotlib. First, two series with the same date range are created. Then each series is plotted on the same graph with their dates on the x-axis. Labels are added for clarity, and a legend is shown. Finally, plt.show() displays the combined plot. This method helps us see how two sets of data change over time side by side.