0
0
Matplotlibdata~10 mins

Dates on x-axis in Matplotlib - Interactive Code Practice

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

Complete the code to import the module needed to work with dates in matplotlib.

Matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as [1]

fig, ax = plt.subplots()
Drag options to blanks, or click blank then click option'
Adates
Btime
Cdatetime
Dcalendar
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'datetime' instead of 'dates' which is a different module.
Trying to import 'time' or 'calendar' which are standard Python modules but not for matplotlib.
2fill in blank
medium

Complete the code to convert a list of date strings into datetime objects for plotting.

Matplotlib
import datetime

dates = ['2024-01-01', '2024-01-02', '2024-01-03']
dates_dt = [datetime.datetime.strptime(date, [1]) for date in dates]
Drag options to blanks, or click blank then click option'
A"%Y/%m/%d"
B"%Y-%m-%d"
C"%d-%m-%Y"
D"%m-%d-%Y"
Attempts:
3 left
💡 Hint
Common Mistakes
Using slashes '/' instead of dashes '-' in the format string.
Mixing up day and month positions.
3fill in blank
hard

Fix the error in the code to plot dates on the x-axis correctly.

Matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime

dates = [datetime.datetime(2024, 1, 1), datetime.datetime(2024, 1, 2), datetime.datetime(2024, 1, 3)]
values = [10, 20, 15]

fig, ax = plt.subplots()
ax.plot(dates, values)
ax.xaxis.set_major_formatter([1])
plt.show()
Drag options to blanks, or click blank then click option'
Amdates.AutoDateLocator()
Bmdates.DateLocator()
Cmdates.DateFormatter('%Y-%m-%d')
Dmdates.DayLocator()
Attempts:
3 left
💡 Hint
Common Mistakes
Using a locator instead of a formatter for setting the date format.
Not setting any formatter, resulting in default or unreadable date labels.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each date string to its datetime object only if the date is after January 1, 2024.

Matplotlib
import datetime

dates = ['2023-12-31', '2024-01-01', '2024-01-02']
date_dict = {date: [1] for date in dates if [2]
Drag options to blanks, or click blank then click option'
Adatetime.datetime.strptime(date, '%Y-%m-%d')
Bdate > '2024-01-01'
Cdatetime.datetime.strptime(date, '%d-%m-%Y')
Ddate < '2024-01-01'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong date format in strptime.
Using the wrong comparison operator or date string.
5fill in blank
hard

Fill all three blanks to create a plot with dates on the x-axis, format the dates as 'Month-Day', and rotate the labels for better readability.

Matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime

# Prepare data
dates = [datetime.datetime(2024, 1, i) for i in range(1, 6)]
values = [5, 7, 6, 8, 7]

fig, ax = plt.subplots()
ax.plot(dates, values)
ax.xaxis.set_major_formatter([1])
plt.setp(ax.get_xticklabels(), rotation=[2], ha=[3])
plt.show()
Drag options to blanks, or click blank then click option'
Amdates.DateFormatter('%b-%d')
B45
C'right'
Dmdates.DateLocator()
Attempts:
3 left
💡 Hint
Common Mistakes
Using a locator instead of a formatter for date format.
Not rotating labels or using wrong rotation angle.
Incorrect horizontal alignment string.