Challenge - 5 Problems
Date Axis Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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()
Attempts:
2 left
💡 Hint
Look at the format string passed to DateFormatter.
✗ Incorrect
The DateFormatter is set with the format '%Y-%m-%d', which means year-month-day with four-digit year, two-digit month, and two-digit day separated by dashes.
❓ data_output
intermediate2: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()
Attempts:
2 left
💡 Hint
Check the interval set in DayLocator and the date range.
✗ Incorrect
DayLocator with interval=2 places a major tick every 2 days starting from the first date. For 5 days, ticks appear on day 1, 3, and 5, totaling 3 ticks.
❓ visualization
advanced2: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?
Attempts:
2 left
💡 Hint
MonthLocator controls tick positions by month, DateFormatter controls label format.
✗ Incorrect
MonthLocator places ticks at the start of each month. DateFormatter with '%b' formats labels as abbreviated month names like 'Jan', 'Feb'.
🔧 Debug
advanced2: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()
Attempts:
2 left
💡 Hint
Matplotlib expects datetime objects or numbers, not date strings.
✗ Incorrect
Matplotlib cannot convert date strings directly to floats for plotting, causing a ValueError.
🚀 Application
expert3: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)
Attempts:
2 left
💡 Hint
MonthLocator with bymonthday=15 places ticks on the 15th of each month within limits.
✗ Incorrect
Between 2024-01-01 and 2024-03-01, the 15th of January and 15th of February fall inside the range, so 2 ticks appear.