0
0
Matplotlibdata~10 mins

Why time series need special handling in Matplotlib - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to plot a time series line chart using matplotlib.

Matplotlib
import matplotlib.pyplot as plt
import pandas as pd

dates = pd.date_range('20230101', periods=5)
values = [10, 12, 15, 14, 13]

plt.plot(dates, values)
plt.xlabel('Date')
plt.ylabel('Value')
plt.title('Time Series Plot')
plt.[1]()
Drag options to blanks, or click blank then click option'
Asavefig
Bclose
Cshow
Dlegend
Attempts:
3 left
💡 Hint
Common Mistakes
Using plt.savefig() instead of plt.show() when you want to display the plot.
Forgetting to call plt.show() so the plot does not appear.
2fill in blank
medium

Complete the code to convert a string column to datetime format in pandas.

Matplotlib
import pandas as pd

data = {'date': ['2023-01-01', '2023-01-02', '2023-01-03'], 'value': [5, 6, 7]}
df = pd.DataFrame(data)
df['date'] = pd.to_datetime(df['[1]'])
Drag options to blanks, or click blank then click option'
Avalue
Bdate
Cindex
Dtime
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to convert a non-date column like 'value'.
Not converting the date column before plotting or analysis.
3fill in blank
hard

Fix the error in the code that resamples time series data to monthly frequency.

Matplotlib
import pandas as pd

dates = pd.date_range('2023-01-01', periods=90)
values = range(90)
df = pd.DataFrame({'value': values}, index=dates)
monthly = df.resample('[1]').mean()
Drag options to blanks, or click blank then click option'
AM
BD
CY
DH
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'D' which means daily, not monthly.
Using 'Y' which means yearly, not monthly.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps dates to values only if the value is greater than 10.

Matplotlib
data = {'2023-01-01': 8, '2023-01-02': 12, '2023-01-03': 15}
filtered = {date: value for date, value in data.items() if value [1] 10}
Drag options to blanks, or click blank then click option'
A==
B<
C!=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which filters values less than 10.
Using '==' which filters values equal to 10.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps dates to values only if the date is after '2023-01-01' and the value is less than 20.

Matplotlib
data = {'2023-01-01': 8, '2023-01-02': 12, '2023-01-03': 25}
filtered = { [1]: [2] for [3] in data.items() if [1] > '2023-01-01' and [2] < 20 }
Drag options to blanks, or click blank then click option'
Adate
Bvalue
Cdate, value
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single variable instead of unpacking the tuple.
Mixing up key and value in the comprehension.