0
0
Matplotlibdata~20 mins

Date locators for tick spacing in Matplotlib - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Date Locator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A4
B5
C3
D10
Attempts:
2 left
💡 Hint
Think about how many days are covered and the interval of 3 days for ticks.
data_output
intermediate
2: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)
A['Jan', 'Feb']
B['Jan', 'Feb', 'Mar']
C['Jan']
D['Feb', 'Mar']
Attempts:
2 left
💡 Hint
Check the date range and which months are included.
visualization
advanced
2: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)
A3
B4
C5
D6
Attempts:
2 left
💡 Hint
Count Mondays every 2 weeks within 30 days.
🔧 Debug
advanced
2: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()
AAttributeError: 'str' object has no attribute 'days'
BNo error, runs successfully
CValueError: interval must be an integer
DTypeError: unsupported operand type(s) for +: 'int' and 'str'
Attempts:
2 left
💡 Hint
Check the type of the interval argument.
🚀 Application
expert
3: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?
AMajor: mdates.MonthLocator() and Minor: mdates.MonthLocator(bymonth=[1,4,7,10])
BMajor: mdates.MonthLocator(bymonth=[1,4,7,10]) and Minor: mdates.MonthLocator()
CMajor: mdates.MonthLocator(bymonth=[1,4,7,10]) and Minor: mdates.MonthLocator(bymonth=[2,3,5,6,8,9,11,12])
DMajor: mdates.MonthLocator(bymonth=[1,4,7,10]) and Minor: mdates.DayLocator(interval=30)
Attempts:
2 left
💡 Hint
Quarters start in months 1,4,7,10. Minor ticks should be other months.