Challenge - 5 Problems
Date Locator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of matplotlib date locator setting
What will be the number of major ticks on the x-axis after running this code snippet?
Matplotlib
import matplotlib.pyplot as plt import matplotlib.dates as mdates import pandas as pd dates = pd.date_range('2023-01-01', periods=10, freq='D') values = range(10) fig, ax = plt.subplots() ax.plot(dates, values) ax.xaxis.set_major_locator(mdates.DayLocator(interval=3)) plt.draw() num_ticks = len(ax.get_xticks()) print(num_ticks)
Attempts:
2 left
💡 Hint
Think about how many days are covered and the interval of 3 days for ticks.
✗ Incorrect
The date range covers 10 days from Jan 1 to Jan 10. Using DayLocator with interval=3 places ticks every 3 days starting from the first date: Jan 1, 4, 7, 10. This results in 4 ticks.
❓ data_output
intermediate2:00remaining
Resulting tick labels with MonthLocator
Given this code, what will be the list of tick labels shown on the x-axis?
Matplotlib
import matplotlib.pyplot as plt import matplotlib.dates as mdates import pandas as pd dates = pd.date_range('2023-01-01', periods=90, freq='D') values = range(90) fig, ax = plt.subplots() ax.plot(dates, values) ax.xaxis.set_major_locator(mdates.MonthLocator()) ax.xaxis.set_major_formatter(mdates.DateFormatter('%b')) plt.draw() labels = [label.get_text() for label in ax.get_xticklabels() if label.get_text()] print(labels)
Attempts:
2 left
💡 Hint
Check the date range and which months are included.
✗ Incorrect
The date range covers 90 days starting Jan 1, 2023, up to Mar 31, including January, February, and March. MonthLocator places ticks at the start of each month: Jan 1, Feb 1, Mar 1. The formatter shows '%b': ['Jan', 'Feb', 'Mar'].
❓ visualization
advanced2:00remaining
Visual effect of using WeekdayLocator with different intervals
Which option shows the correct number of major ticks on the x-axis when using WeekdayLocator with interval=2 on a 30-day date range?
Matplotlib
import matplotlib.pyplot as plt import matplotlib.dates as mdates import pandas as pd dates = pd.date_range('2023-01-01', periods=30, freq='D') values = range(30) fig, ax = plt.subplots() ax.plot(dates, values) ax.xaxis.set_major_locator(mdates.WeekdayLocator(byweekday=mdates.MO, interval=2)) plt.draw() num_ticks = len(ax.get_xticks()) print(num_ticks)
Attempts:
2 left
💡 Hint
Count Mondays every 2 weeks within 30 days.
✗ Incorrect
Jan 1, 2023 is Sunday. Mondays: Jan 2, 9, 16, 23, 30. WeekdayLocator(byweekday=MO, interval=2) places ticks every 2 weeks on Mondays starting from the first: Jan 2, 16, 30. Total 3 ticks.
🔧 Debug
advanced2:00remaining
Identify the error in this date locator usage
What error will this code raise when setting the major locator?
Matplotlib
import matplotlib.pyplot as plt import matplotlib.dates as mdates import pandas as pd dates = pd.date_range('2023-01-01', periods=5, freq='D') values = range(5) fig, ax = plt.subplots() ax.plot(dates, values) ax.xaxis.set_major_locator(mdates.MonthLocator(interval='2')) plt.show()
Attempts:
2 left
💡 Hint
Check the type of the interval argument.
✗ Incorrect
MonthLocator expects an integer for interval. Passing '2' (string) causes a TypeError during tick computation when adding it to an integer (e.g., month calculation).
🚀 Application
expert3:00remaining
Choosing the correct locator for quarterly data visualization
You have a time series dataset with daily data spanning multiple years. You want to place major ticks at the start of each quarter and minor ticks at the start of each month. Which combination of locators should you use?
Attempts:
2 left
💡 Hint
Quarters start in months 1,4,7,10. Minor ticks should be other months.
✗ Incorrect
To place major ticks at the start of each quarter, use MonthLocator with bymonth=[1,4,7,10]. For minor ticks at the start of other months, use MonthLocator with bymonth set to the remaining months. This ensures clear separation of major and minor ticks.