Bird
Raised Fist0
Matplotlibdata~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of plotting multiple time series on the same graph using matplotlib?
easy
A. To make the graph load faster
B. To create separate graphs for each time series
C. To hide the data labels for clarity
D. To visually compare trends and differences between series over time

Solution

  1. Step 1: Understand the purpose of multiple time series plots

    Plotting multiple lines on one graph helps us see how different series change over time side by side.
  2. Step 2: Compare options with this purpose

    Only To visually compare trends and differences between series over time describes this visual comparison goal correctly.
  3. Final Answer:

    To visually compare trends and differences between series over time -> Option D
  4. Quick Check:

    Multiple time series comparison = Visual comparison [OK]
Hint: Multiple lines on one plot show trends side by side [OK]
Common Mistakes:
  • Thinking multiple plots are created instead of one combined plot
  • Ignoring the importance of labels and legends
  • Believing it speeds up graph loading
2. Which of the following matplotlib code snippets correctly plots two time series series1 and series2 on the same graph with labels?
easy
A. plt.plot(series1, label='Series 1') plt.legend() plt.show()
B. plt.plot(series1) plt.plot(series2) plt.show()
C. plt.plot(series1, label='Series 1') plt.plot(series2, label='Series 2') plt.legend() plt.show()
D. plt.plot(series1, label='Series 1') plt.plot(series2, label='Series 2') plt.show()

Solution

  1. Step 1: Check for plotting both series with labels

    plt.plot(series1, label='Series 1') plt.plot(series2, label='Series 2') plt.legend() plt.show() plots both series and adds labels for each.
  2. Step 2: Verify legend call to show labels

    plt.plot(series1, label='Series 1') plt.plot(series2, label='Series 2') plt.legend() plt.show() calls plt.legend() to display labels on the graph.
  3. Final Answer:

    Code that plots both series with labels and shows legend -> Option C
  4. Quick Check:

    Plot both + labels + legend = plt.plot(series1, label='Series 1') plt.plot(series2, label='Series 2') plt.legend() plt.show() [OK]
Hint: Always add labels and call plt.legend() to show them [OK]
Common Mistakes:
  • Forgetting to call plt.legend() so labels don't appear
  • Plotting only one series instead of both
  • Not adding labels to each series
3. What will be the output of this code snippet?
import matplotlib.pyplot as plt
series1 = [1, 3, 2, 4]
series2 = [2, 1, 3, 5]
plt.plot(series1, label='A')
plt.plot(series2, label='B')
plt.legend()
plt.show()
medium
A. A graph with two lines labeled 'A' and 'B' showing series1 and series2
B. A graph with one line showing the sum of series1 and series2
C. A graph with two lines but no labels
D. SyntaxError due to missing import

Solution

  1. Step 1: Analyze the plotting commands

    Two lines are plotted: series1 with label 'A' and series2 with label 'B'.
  2. Step 2: Check legend and show calls

    Calling plt.legend() displays the labels on the graph.
  3. Final Answer:

    A graph with two lines labeled 'A' and 'B' showing series1 and series2 -> Option A
  4. Quick Check:

    Two labeled lines plotted = A graph with two lines labeled 'A' and 'B' showing series1 and series2 [OK]
Hint: Labels appear only if plt.legend() is called [OK]
Common Mistakes:
  • Thinking lines are summed instead of plotted separately
  • Forgetting plt.legend() hides labels
  • Assuming syntax error without import
4. The following code is intended to plot two time series with labels, but the legend does not appear. What is the error?
import matplotlib.pyplot as plt
series1 = [1, 2, 3]
series2 = [3, 2, 1]
plt.plot(series1, label='First')
plt.plot(series2, label='Second')
plt.show()
medium
A. Missing plt.legend() call to display the legend
B. Labels are incorrectly assigned inside plt.plot()
C. plt.show() should be called before plotting
D. series2 should be plotted before series1

Solution

  1. Step 1: Check for legend display code

    The code does not call plt.legend(), so labels won't show.
  2. Step 2: Confirm label usage is correct

    Labels are correctly passed in plt.plot(), so no issue there.
  3. Final Answer:

    Missing plt.legend() call to display the legend -> Option A
  4. Quick Check:

    Legend missing without plt.legend() [OK]
Hint: Always add plt.legend() to show labels on plot [OK]
Common Mistakes:
  • Assuming labels show automatically without legend call
  • Thinking order of plotting affects legend display
  • Calling plt.show() too early
5. You have three time series with different lengths:
series1 = [1, 2, 3, 4], series2 = [2, 3, 4], series3 = [1, 3, 5, 7, 9].
How can you correctly plot all three on the same graph to compare them over time?
hard
A. Plot all series using the length of the longest series only
B. Plot each series with its own x-axis range matching its length, then add labels and legend
C. Pad shorter series with zeros to match the longest series length before plotting
D. Plot only the shortest series to avoid errors

Solution

  1. Step 1: Understand different lengths require matching x-axis

    Each series must be plotted with x values matching its length to avoid misalignment.
  2. Step 2: Plot each series with its own x range and add labels

    Use range(len(series)) for x-axis, plot all, then add labels and legend.
  3. Final Answer:

    Plot each series with its own x-axis range matching its length, then add labels and legend -> Option B
  4. Quick Check:

    Match x-axis length per series + labels = Plot each series with its own x-axis range matching its length, then add labels and legend [OK]
Hint: Use range(len(series)) for x-axis of each series [OK]
Common Mistakes:
  • Plotting all series without adjusting x-axis causes misaligned lines
  • Padding with zeros changes data meaning
  • Ignoring series length differences
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard