Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to plot a time series line using matplotlib.
Matplotlib
import matplotlib.pyplot as plt import pandas as pd dates = pd.date_range('2024-01-01', periods=5) values = [10, 12, 9, 14, 13] plt.plot(dates, [1]) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dates' as the y-axis data instead of 'values'.
Using a range or incomplete list instead of the full values list.
✗ Incorrect
The y-axis data for the plot should be the 'values' list, which contains the time series data points.
2fill in blank
mediumComplete the code to fill the area under the time series line.
Matplotlib
plt.plot(dates, values) plt.[1](dates, values, alpha=0.3) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fill' which is not a matplotlib function for this purpose.
Using 'bar' or 'scatter' which create different plot types.
✗ Incorrect
The 'fill_between' function fills the area between the x-axis and the line defined by the y-values.
3fill in blank
hardFix the error in the code to fill between two time series lines.
Matplotlib
values2 = [8, 11, 10, 13, 12] plt.plot(dates, values) plt.plot(dates, values2) plt.fill_between(dates, values, [1], color='gray', alpha=0.2) plt.show()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dates' or 'range(5)' which are x-axis values, not y-values.
Using the same 'values' list twice, which fills under the same line.
✗ Incorrect
To fill between two lines, the third argument to fill_between should be the second y-values list, here 'values2'.
4fill in blank
hardFill the blanks to create a dictionary comprehension that maps dates to values only if the value is greater than 10.
Matplotlib
filtered = { [1]: [2] for [3], [4] in zip(dates, values) if [2] > 10 } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable name for both key and value.
Not matching variable names in the for loop and the key:value pair.
✗ Incorrect
The dictionary comprehension maps each date to its value if the value is greater than 10. The variables in the loop are 'date' and 'value'.
5fill in blank
hardFill the blanks to create a dictionary comprehension that maps uppercase dates (as strings) to values only if the value is less than 13.
Matplotlib
result = { [1]: [2] for [3], [4] in zip(dates, values) if [2] < 13 } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not converting the date to string before calling upper().
Using inconsistent variable names.
✗ Incorrect
The comprehension converts dates to uppercase strings as keys and maps them to values less than 13.