0
0
Matplotlibdata~20 mins

Dates on x-axis in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Date Axis Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this matplotlib date plot code?
Consider the following Python code that plots dates on the x-axis using matplotlib. What will be the format of the x-axis labels shown in the plot?
Matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime

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

fig, ax = plt.subplots()
ax.plot(dates, values)
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.show()
ADates formatted as 'YYYY-MM-DD', e.g., '2024-01-01', '2024-01-02', '2024-01-03'
BDates formatted as 'MM/DD/YYYY', e.g., '01/01/2024', '01/02/2024', '01/03/2024'
CDates formatted as 'Day Month Year', e.g., '1 Jan 2024', '2 Jan 2024', '3 Jan 2024'
DDates shown as integers representing days since 1970-01-01
Attempts:
2 left
💡 Hint
Look at the format string passed to DateFormatter.
data_output
intermediate
2:00remaining
How many major ticks appear on the x-axis?
Given this code that plots data with dates on the x-axis, how many major ticks will appear on the x-axis?
Matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime

dates = [datetime(2024, 5, 1), datetime(2024, 5, 2), datetime(2024, 5, 3), datetime(2024, 5, 4), datetime(2024, 5, 5)]
values = [5, 3, 6, 2, 7]

fig, ax = plt.subplots()
ax.plot(dates, values)
ax.xaxis.set_major_locator(mdates.DayLocator(interval=2))
plt.show()
A3 major ticks
B5 major ticks
C2 major ticks
D1 major tick
Attempts:
2 left
💡 Hint
Check the interval set in DayLocator and the date range.
visualization
advanced
2:30remaining
Which plot shows correctly formatted monthly ticks on the x-axis?
You want to plot data with monthly ticks labeled as abbreviated month names (e.g., Jan, Feb). Which code snippet produces this correctly formatted x-axis?
A
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
B
ax.xaxis.set_major_locator(mdates.WeekdayLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))
C
ax.xaxis.set_major_locator(mdates.DayLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))
D
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))
Attempts:
2 left
💡 Hint
MonthLocator controls tick positions by month, DateFormatter controls label format.
🔧 Debug
advanced
2:00remaining
What error does this code raise when plotting dates?
This code attempts to plot dates on the x-axis but raises an error. What is the error type?
Matplotlib
import matplotlib.pyplot as plt
from datetime import datetime

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

plt.plot(dates, values)
plt.show()
ATypeError: float() argument must be a string or a number, not 'datetime.date'
BNo error, plot displays correctly
CValueError: could not convert string to float: '2024-01-01'
DAttributeError: 'str' object has no attribute 'toordinal'
Attempts:
2 left
💡 Hint
Matplotlib expects datetime objects or numbers, not date strings.
🚀 Application
expert
3:00remaining
What is the value of 'num_ticks' after running this code?
This code sets up a plot with dates on the x-axis and custom tick intervals. What is the number of major ticks on the x-axis after running?
Matplotlib
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime

start = datetime(2024, 1, 1)
end = datetime(2024, 3, 1)

fig, ax = plt.subplots()
ax.set_xlim(start, end)
ax.xaxis.set_major_locator(mdates.MonthLocator(bymonthday=15))

# Count major ticks
num_ticks = len(ax.get_xticks())
plt.close(fig)
A1
B2
C3
D0
Attempts:
2 left
💡 Hint
MonthLocator with bymonthday=15 places ticks on the 15th of each month within limits.