Complete the code to import the matplotlib plotting module.
import [1].pyplot as plt
We import matplotlib.pyplot as plt to create plots.
Complete the code to create a date range from January 1, 2023 to January 10, 2023 using pandas.
dates = pd.date_range(start='2023-01-01', end=[1])
The end parameter defines the last date in the range. We want January 10, 2023.
Fix the error in the code to highlight the date range between '2023-01-03' and '2023-01-06' on the plot.
plt.axvspan(pd.to_datetime('2023-01-03'), [1], color='yellow', alpha=0.3)
The axvspan function needs datetime objects for the start and end. We convert the end date string to datetime with pd.to_datetime.
Fill both blanks to create a dictionary comprehension that maps each date to its value only if the value is greater than 50.
highlighted = {date: value for date, value in data.items() if value [1] 50 and date [2] pd.to_datetime('2023-01-05')}We want values greater than 50 and dates before January 5, 2023, so use > and <.
Fill all three blanks to create a plot with highlighted date ranges and labels.
plt.plot(dates, values) plt.axvspan([1], [2], color='green', alpha=0.2) plt.text([3], max(values), 'Highlight', verticalalignment='bottom') plt.show()
The highlighted range is from January 5 to January 7, and the label is placed at January 3.