This code plots two time series with dates on the x-axis and values on the y-axis. Markers help distinguish points. The legend shows which line is which.
Matplotlib
import matplotlib.pyplot as plt
import pandas as pd
# Create sample dates
dates = pd.date_range(start='2024-01-01', periods=5, freq='D')
# Sample data for two time series
series1 = [100, 120, 130, 115, 140]
series2 = [90, 110, 125, 130, 135]
# Plot both series
plt.plot(dates, series1, marker='o', label='Series 1')
plt.plot(dates, series2, marker='s', label='Series 2')
# Add labels and title
plt.xlabel('Date')
plt.ylabel('Value')
plt.title('Multiple Time Series Comparison')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
OutputSuccess
Important Notes
Make sure time points align or are comparable for meaningful comparison.
Use different colors or markers to clearly see each series.
Adding grid lines helps read values better.
Summary
Plot multiple time series on the same chart using plt.plot() repeatedly.
Label each series for clarity with label and show legend.
Use axis labels and titles to explain what the chart shows.
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
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.
Step 2: Compare options with this purpose
Only To visually compare trends and differences between series over time describes this visual comparison goal correctly.
Final Answer:
To visually compare trends and differences between series over time -> Option D
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
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.
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.
Final Answer:
Code that plots both series with labels and shows legend -> Option C
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
Step 1: Check for legend display code
The code does not call plt.legend(), so labels won't show.
Step 2: Confirm label usage is correct
Labels are correctly passed in plt.plot(), so no issue there.
Final Answer:
Missing plt.legend() call to display the legend -> Option A
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
Step 1: Understand different lengths require matching x-axis
Each series must be plotted with x values matching its length to avoid misalignment.
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.
Final Answer:
Plot each series with its own x-axis range matching its length, then add labels and legend -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.