0
0
Matplotlibdata~20 mins

Date formatting with mdates in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Date Formatting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the x-axis date format shown by this code?
This code plots dates on the x-axis using matplotlib and mdates. What date format will appear on the x-axis labels?
Matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime

dates = [datetime.datetime(2024, 1, i+1) for i in range(5)]
values = [10, 20, 15, 25, 30]

fig, ax = plt.subplots()
ax.plot(dates, values)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.show()
ADates formatted as 'DD-MM-YYYY', e.g., '01-01-2024'
BDates formatted as 'MM/DD/YYYY', e.g., '01/01/2024'
CDates formatted as 'Month day, Year', e.g., 'January 1, 2024'
DDates formatted as 'YYYY-MM-DD', e.g., '2024-01-01'
Attempts:
2 left
💡 Hint
Look at the string inside DateFormatter to see the date format pattern.
data_output
intermediate
2:00remaining
How many major ticks appear on the x-axis?
Given this code that plots 10 daily dates and sets the major locator to mdates.DayLocator(interval=3), how many major ticks will appear on the x-axis?
Matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime

dates = [datetime.datetime(2024, 3, i+1) for i in range(10)]
values = range(10)

fig, ax = plt.subplots()
ax.plot(dates, values)
ax.xaxis.set_major_locator(mdates.DayLocator(interval=3))
plt.close(fig)  # Close plot to avoid display
A4
B3
C5
D10
Attempts:
2 left
💡 Hint
Count days from 1 to 10 with steps of 3: days 1,4,7,10.
🔧 Debug
advanced
2:00remaining
Why does this code raise a TypeError?
This code tries to format dates on the x-axis but raises a TypeError. What is the cause?
Matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import datetime

dates = ['2024-01-01', '2024-01-02', '2024-01-03']
values = [1, 2, 3]

fig, ax = plt.subplots()
ax.plot(dates, values)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.show()
ADates are strings, but DateFormatter expects datetime objects, causing TypeError.
BThe DateFormatter format string '%Y-%m-%d' is invalid and causes TypeError.
CThe plot function cannot accept lists as x-axis values, causing TypeError.
DMissing plt.show() causes TypeError when rendering the plot.
Attempts:
2 left
💡 Hint
Check the type of the dates list elements.
visualization
advanced
2:00remaining
Which option correctly sets monthly ticks with abbreviated month names?
You want the x-axis to show ticks at the start of each month with labels like 'Jan', 'Feb', etc. Which code snippet achieves this?
A
ax.xaxis.set_major_locator(mdates.WeekdayLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))
B
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))
C
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%B'))
D
ax.xaxis.set_major_locator(mdates.DayLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))
Attempts:
2 left
💡 Hint
Look for MonthLocator and '%b' for abbreviated month names.
🧠 Conceptual
expert
2:00remaining
What happens if you set both major and minor locators with mdates but only format major ticks?
If you set ax.xaxis.set_major_locator(mdates.MonthLocator()) and ax.xaxis.set_minor_locator(mdates.WeekdayLocator()), but only set a major formatter with DateFormatter, what will the minor ticks show?
AMinor ticks cause an error because they lack a formatter.
BMinor ticks show the same labels as major ticks, causing label overlap.
CMinor ticks appear without labels because no formatter is set for them.
DMinor ticks are hidden automatically if no formatter is set.
Attempts:
2 left
💡 Hint
Think about how matplotlib handles tick labels when no formatter is assigned.