0
0
Matplotlibdata~20 mins

Multiple time series comparison in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multiple Time Series Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
visualization
intermediate
2:00remaining
Identify the correct plot for multiple time series

Given three time series data sets, which plot correctly shows all three series on the same graph with distinct colors and a legend?

Matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

dates = pd.date_range('2023-01-01', periods=5)
data1 = np.array([1, 3, 2, 5, 4])
data2 = np.array([2, 2, 3, 4, 5])
data3 = np.array([5, 3, 4, 2, 1])
A
plt.scatter(dates, data1, label='Series 1')
plt.scatter(dates, data2, label='Series 2')
plt.scatter(dates, data3, label='Series 3')
plt.legend()
plt.show()
B
plt.plot(dates, data1)
plt.plot(dates, data2)
plt.plot(dates, data3)
plt.show()
C
plt.plot(data1, label='Series 1')
plt.plot(data2, label='Series 2')
plt.plot(data3, label='Series 3')
plt.legend()
plt.show()
D
plt.plot(dates, data1, label='Series 1')
plt.plot(dates, data2, label='Series 2')
plt.plot(dates, data3, label='Series 3')
plt.legend()
plt.show()
Attempts:
2 left
💡 Hint

Look for a line plot that uses dates on the x-axis and includes a legend.

Predict Output
intermediate
1:30remaining
Output of multiple time series plot code

What will be the output type of the following code snippet?

Matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

dates = pd.date_range('2023-01-01', periods=3)
data1 = [1, 2, 3]
data2 = [3, 2, 1]

fig, ax = plt.subplots()
ax.plot(dates, data1, label='A')
ax.plot(dates, data2, label='B')
ax.legend()
plt.show()
AA scatter plot with points for data1 and data2
BA line plot with two lines labeled 'A' and 'B' over the dates
CA bar chart comparing data1 and data2
DA pie chart showing proportions of data1 and data2
Attempts:
2 left
💡 Hint

Look at the ax.plot calls and the use of legend().

data_output
advanced
2:00remaining
Resulting DataFrame after merging multiple time series

Given two time series DataFrames with different date ranges, what is the shape of the merged DataFrame when merged on date with an outer join?

Matplotlib
import pandas as pd

df1 = pd.DataFrame({'date': pd.date_range('2023-01-01', periods=3), 'value1': [10, 20, 30]})
df2 = pd.DataFrame({'date': pd.date_range('2023-01-02', periods=3), 'value2': [15, 25, 35]})
merged = pd.merge(df1, df2, on='date', how='outer')
A(4, 3)
B(6, 3)
C(5, 3)
D(3, 3)
Attempts:
2 left
💡 Hint

Count unique dates after combining both date ranges with outer join.

🔧 Debug
advanced
1:30remaining
Identify the error in multiple time series plotting code

What error will this code raise?

Matplotlib
import matplotlib.pyplot as plt
import pandas as pd

dates = pd.date_range('2023-01-01', periods=4)
data1 = [1, 2, 3]
data2 = [4, 5, 6, 7]

plt.plot(dates, data1, label='Series 1')
plt.plot(dates, data2, label='Series 2')
plt.legend()
plt.show()
AValueError: x and y must have same first dimension
BTypeError: unsupported operand type(s) for +: 'int' and 'str'
CNo error, plots successfully
DIndexError: list index out of range
Attempts:
2 left
💡 Hint

Check if the x and y data lengths match for each plot call.

🚀 Application
expert
2:30remaining
Calculate correlation between multiple time series

You have three time series of equal length. Which code correctly calculates the pairwise Pearson correlation matrix?

Matplotlib
import pandas as pd
import numpy as np

np.random.seed(0)
data = pd.DataFrame({
    'A': np.random.randn(5),
    'B': np.random.randn(5),
    'C': np.random.randn(5)
})
Adata.corrwith(data['A'])
Bdata.cov()
Cdata.corr(method='pearson')
Ddata.corr(method='spearman')
Attempts:
2 left
💡 Hint

Look for the method that computes Pearson correlation matrix for all columns.