Challenge - 5 Problems
Date Formatting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Look at the string inside DateFormatter to see the date format pattern.
✗ Incorrect
The DateFormatter is set with '%Y-%m-%d', which means year-month-day with dashes, so the dates appear like '2024-01-01'.
❓ data_output
intermediate2: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
Attempts:
2 left
💡 Hint
Count days from 1 to 10 with steps of 3: days 1,4,7,10.
✗ Incorrect
DayLocator with interval=3 places ticks every 3 days starting from the first date, so ticks appear on days 1,4,7,10, totaling 4 ticks.
🔧 Debug
advanced2: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()
Attempts:
2 left
💡 Hint
Check the type of the dates list elements.
✗ Incorrect
DateFormatter expects datetime objects to format dates. Passing strings causes a TypeError because it cannot format string objects as dates.
❓ visualization
advanced2: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?
Attempts:
2 left
💡 Hint
Look for MonthLocator and '%b' for abbreviated month names.
✗ Incorrect
MonthLocator sets ticks at month starts. DateFormatter with '%b' formats months as abbreviated names like 'Jan'.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about how matplotlib handles tick labels when no formatter is assigned.
✗ Incorrect
Minor ticks appear as marks but have no labels if no formatter is assigned. Only major ticks get labels from the major formatter.