Skip to content

Commit 982254c

Browse files
authored
Merge pull request #9841 from jklymak/fix-years-autodate
ENH: make interval_multiples work for years
2 parents c608ae1 + 242134d commit 982254c

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed

lib/matplotlib/dates.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,11 @@
9696
<../gallery/ticks_and_spines/date_demo_rrule.html>`_.
9797
9898
* :class:`AutoDateLocator`: On autoscale, this class picks the best
99-
:class:`MultipleDateLocator` to set the view limits and the tick
100-
locations.
99+
:class:`RRuleLocator` to set the view limits and the tick
100+
locations. If called with ``interval_multiples=True`` it will
101+
make ticks line up with sensible multiples of the tick intervals. E.g.
102+
if the interval is 4 hours, it will pick hours 0, 4, 8, etc as ticks.
103+
This behaviour is not garaunteed by default.
101104
102105
Date formatters
103106
---------------
@@ -1189,15 +1192,15 @@ def get_locator(self, dmin, dmax):
11891192
else:
11901193
byranges[i] = self._byranges[i]
11911194

1192-
# We found what frequency to use
11931195
break
11941196
else:
11951197
raise ValueError('No sensible date limit could be found in the '
11961198
'AutoDateLocator.')
11971199

1198-
if use_rrule_locator[i]:
1200+
if (freq == YEARLY) and self.interval_multiples:
1201+
locator = YearLocator(interval)
1202+
elif use_rrule_locator[i]:
11991203
_, bymonth, bymonthday, byhour, byminute, bysecond, _ = byranges
1200-
12011204
rrule = rrulewrapper(self._freq, interval=interval,
12021205
dtstart=dmin, until=dmax,
12031206
bymonth=bymonth, bymonthday=bymonthday,

lib/matplotlib/tests/test_dates.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,79 @@ def _create_auto_date_locator(date1, date2):
380380
assert list(map(str, mdates.num2date(locator()))) == expected
381381

382382

383+
def test_auto_date_locator_intmult():
384+
def _create_auto_date_locator(date1, date2):
385+
locator = mdates.AutoDateLocator(interval_multiples=True)
386+
locator.create_dummy_axis()
387+
locator.set_view_interval(mdates.date2num(date1),
388+
mdates.date2num(date2))
389+
return locator
390+
391+
d1 = datetime.datetime(1997, 1, 1)
392+
results = ([datetime.timedelta(weeks=52 * 200),
393+
['1980-01-01 00:00:00+00:00', '2000-01-01 00:00:00+00:00',
394+
'2020-01-01 00:00:00+00:00', '2040-01-01 00:00:00+00:00',
395+
'2060-01-01 00:00:00+00:00', '2080-01-01 00:00:00+00:00',
396+
'2100-01-01 00:00:00+00:00', '2120-01-01 00:00:00+00:00',
397+
'2140-01-01 00:00:00+00:00', '2160-01-01 00:00:00+00:00',
398+
'2180-01-01 00:00:00+00:00', '2200-01-01 00:00:00+00:00']
399+
],
400+
[datetime.timedelta(weeks=52),
401+
['1997-01-01 00:00:00+00:00', '1997-02-01 00:00:00+00:00',
402+
'1997-03-01 00:00:00+00:00', '1997-04-01 00:00:00+00:00',
403+
'1997-05-01 00:00:00+00:00', '1997-06-01 00:00:00+00:00',
404+
'1997-07-01 00:00:00+00:00', '1997-08-01 00:00:00+00:00',
405+
'1997-09-01 00:00:00+00:00', '1997-10-01 00:00:00+00:00',
406+
'1997-11-01 00:00:00+00:00', '1997-12-01 00:00:00+00:00']
407+
],
408+
[datetime.timedelta(days=141),
409+
['1997-01-01 00:00:00+00:00', '1997-01-22 00:00:00+00:00',
410+
'1997-02-01 00:00:00+00:00', '1997-02-22 00:00:00+00:00',
411+
'1997-03-01 00:00:00+00:00', '1997-03-22 00:00:00+00:00',
412+
'1997-04-01 00:00:00+00:00', '1997-04-22 00:00:00+00:00',
413+
'1997-05-01 00:00:00+00:00', '1997-05-22 00:00:00+00:00']
414+
],
415+
[datetime.timedelta(days=40),
416+
['1997-01-01 00:00:00+00:00', '1997-01-08 00:00:00+00:00',
417+
'1997-01-15 00:00:00+00:00', '1997-01-22 00:00:00+00:00',
418+
'1997-01-29 00:00:00+00:00', '1997-02-01 00:00:00+00:00',
419+
'1997-02-08 00:00:00+00:00']
420+
],
421+
[datetime.timedelta(hours=40),
422+
['1997-01-01 00:00:00+00:00', '1997-01-01 04:00:00+00:00',
423+
'1997-01-01 08:00:00+00:00', '1997-01-01 12:00:00+00:00',
424+
'1997-01-01 16:00:00+00:00', '1997-01-01 20:00:00+00:00',
425+
'1997-01-02 00:00:00+00:00', '1997-01-02 04:00:00+00:00',
426+
'1997-01-02 08:00:00+00:00', '1997-01-02 12:00:00+00:00',
427+
'1997-01-02 16:00:00+00:00']
428+
],
429+
[datetime.timedelta(minutes=20),
430+
['1997-01-01 00:00:00+00:00', '1997-01-01 00:05:00+00:00',
431+
'1997-01-01 00:10:00+00:00', '1997-01-01 00:15:00+00:00',
432+
'1997-01-01 00:20:00+00:00']
433+
],
434+
[datetime.timedelta(seconds=40),
435+
['1997-01-01 00:00:00+00:00', '1997-01-01 00:00:05+00:00',
436+
'1997-01-01 00:00:10+00:00', '1997-01-01 00:00:15+00:00',
437+
'1997-01-01 00:00:20+00:00', '1997-01-01 00:00:25+00:00',
438+
'1997-01-01 00:00:30+00:00', '1997-01-01 00:00:35+00:00',
439+
'1997-01-01 00:00:40+00:00']
440+
],
441+
[datetime.timedelta(microseconds=1500),
442+
['1996-12-31 23:59:59.999507+00:00',
443+
'1997-01-01 00:00:00+00:00',
444+
'1997-01-01 00:00:00.000502+00:00',
445+
'1997-01-01 00:00:00.001005+00:00',
446+
'1997-01-01 00:00:00.001508+00:00']
447+
],
448+
)
449+
450+
for t_delta, expected in results:
451+
d2 = d1 + t_delta
452+
locator = _create_auto_date_locator(d1, d2)
453+
assert list(map(str, mdates.num2date(locator()))) == expected
454+
455+
383456
@image_comparison(baseline_images=['date_inverted_limit'],
384457
extensions=['png'])
385458
def test_date_inverted_limit():

0 commit comments

Comments
 (0)