|
12 | 12 | from matplotlib.cbook import MatplotlibDeprecationWarning
|
13 | 13 | import matplotlib.dates as mdates
|
14 | 14 | import matplotlib.ticker as mticker
|
| 15 | +from matplotlib import rc_context |
15 | 16 |
|
16 | 17 |
|
17 | 18 | def __has_pytz():
|
@@ -439,7 +440,6 @@ def _create_auto_date_locator(date1, date2):
|
439 | 440 | mdates.date2num(date2))
|
440 | 441 | return locator
|
441 | 442 |
|
442 |
| - d1 = datetime.datetime(1997, 1, 1) |
443 | 443 | results = ([datetime.timedelta(weeks=52 * 200),
|
444 | 444 | ['1980-01-01 00:00:00+00:00', '2000-01-01 00:00:00+00:00',
|
445 | 445 | '2020-01-01 00:00:00+00:00', '2040-01-01 00:00:00+00:00',
|
@@ -500,6 +500,7 @@ def _create_auto_date_locator(date1, date2):
|
500 | 500 | ],
|
501 | 501 | )
|
502 | 502 |
|
| 503 | + d1 = datetime.datetime(1997, 1, 1) |
503 | 504 | for t_delta, expected in results:
|
504 | 505 | d2 = d1 + t_delta
|
505 | 506 | locator = _create_auto_date_locator(d1, d2)
|
@@ -557,6 +558,77 @@ def _create_auto_date_locator(date1, date2):
|
557 | 558 | assert strings == expected
|
558 | 559 |
|
559 | 560 |
|
| 561 | +def test_auto_date_locator_intmult_tz(): |
| 562 | + def _create_auto_date_locator(date1, date2, tz): |
| 563 | + locator = mdates.AutoDateLocator(interval_multiples=True, tz=tz) |
| 564 | + locator.create_dummy_axis() |
| 565 | + locator.set_view_interval(mdates.date2num(date1), |
| 566 | + mdates.date2num(date2)) |
| 567 | + return locator |
| 568 | + |
| 569 | + results = ([datetime.timedelta(weeks=52*200), |
| 570 | + ['1980-01-01 00:00:00-08:00', '2000-01-01 00:00:00-08:00', |
| 571 | + '2020-01-01 00:00:00-08:00', '2040-01-01 00:00:00-08:00', |
| 572 | + '2060-01-01 00:00:00-08:00', '2080-01-01 00:00:00-08:00', |
| 573 | + '2100-01-01 00:00:00-08:00', '2120-01-01 00:00:00-08:00', |
| 574 | + '2140-01-01 00:00:00-08:00', '2160-01-01 00:00:00-08:00', |
| 575 | + '2180-01-01 00:00:00-08:00', '2200-01-01 00:00:00-08:00'] |
| 576 | + ], |
| 577 | + [datetime.timedelta(weeks=52), |
| 578 | + ['1997-01-01 00:00:00-08:00', '1997-02-01 00:00:00-08:00', |
| 579 | + '1997-03-01 00:00:00-08:00', '1997-04-01 00:00:00-08:00', |
| 580 | + '1997-05-01 00:00:00-07:00', '1997-06-01 00:00:00-07:00', |
| 581 | + '1997-07-01 00:00:00-07:00', '1997-08-01 00:00:00-07:00', |
| 582 | + '1997-09-01 00:00:00-07:00', '1997-10-01 00:00:00-07:00', |
| 583 | + '1997-11-01 00:00:00-08:00', '1997-12-01 00:00:00-08:00'] |
| 584 | + ], |
| 585 | + [datetime.timedelta(days=141), |
| 586 | + ['1997-01-01 00:00:00-08:00', '1997-01-22 00:00:00-08:00', |
| 587 | + '1997-02-01 00:00:00-08:00', '1997-02-22 00:00:00-08:00', |
| 588 | + '1997-03-01 00:00:00-08:00', '1997-03-22 00:00:00-08:00', |
| 589 | + '1997-04-01 00:00:00-08:00', '1997-04-22 00:00:00-07:00', |
| 590 | + '1997-05-01 00:00:00-07:00', '1997-05-22 00:00:00-07:00'] |
| 591 | + ], |
| 592 | + [datetime.timedelta(days=40), |
| 593 | + ['1997-01-01 00:00:00-08:00', '1997-01-05 00:00:00-08:00', |
| 594 | + '1997-01-09 00:00:00-08:00', '1997-01-13 00:00:00-08:00', |
| 595 | + '1997-01-17 00:00:00-08:00', '1997-01-21 00:00:00-08:00', |
| 596 | + '1997-01-25 00:00:00-08:00', '1997-01-29 00:00:00-08:00', |
| 597 | + '1997-02-01 00:00:00-08:00', '1997-02-05 00:00:00-08:00', |
| 598 | + '1997-02-09 00:00:00-08:00'] |
| 599 | + ], |
| 600 | + [datetime.timedelta(hours=40), |
| 601 | + ['1997-01-01 00:00:00-08:00', '1997-01-01 04:00:00-08:00', |
| 602 | + '1997-01-01 08:00:00-08:00', '1997-01-01 12:00:00-08:00', |
| 603 | + '1997-01-01 16:00:00-08:00', '1997-01-01 20:00:00-08:00', |
| 604 | + '1997-01-02 00:00:00-08:00', '1997-01-02 04:00:00-08:00', |
| 605 | + '1997-01-02 08:00:00-08:00', '1997-01-02 12:00:00-08:00', |
| 606 | + '1997-01-02 16:00:00-08:00'] |
| 607 | + ], |
| 608 | + [datetime.timedelta(minutes=20), |
| 609 | + ['1997-01-01 00:00:00-08:00', '1997-01-01 00:05:00-08:00', |
| 610 | + '1997-01-01 00:10:00-08:00', '1997-01-01 00:15:00-08:00', |
| 611 | + '1997-01-01 00:20:00-08:00'] |
| 612 | + ], |
| 613 | + [datetime.timedelta(seconds=40), |
| 614 | + ['1997-01-01 00:00:00-08:00', '1997-01-01 00:00:05-08:00', |
| 615 | + '1997-01-01 00:00:10-08:00', '1997-01-01 00:00:15-08:00', |
| 616 | + '1997-01-01 00:00:20-08:00', '1997-01-01 00:00:25-08:00', |
| 617 | + '1997-01-01 00:00:30-08:00', '1997-01-01 00:00:35-08:00', |
| 618 | + '1997-01-01 00:00:40-08:00'] |
| 619 | + ] |
| 620 | + ) |
| 621 | + |
| 622 | + tz = dateutil.tz.gettz('Canada/Pacific') |
| 623 | + d1 = datetime.datetime(1997, 1, 1, tzinfo=tz) |
| 624 | + for t_delta, expected in results: |
| 625 | + with rc_context({'_internal.classic_mode': False}): |
| 626 | + d2 = d1 + t_delta |
| 627 | + locator = _create_auto_date_locator(d1, d2, tz) |
| 628 | + st = list(map(str, mdates.num2date(locator(), tz=tz))) |
| 629 | + assert st == expected |
| 630 | + |
| 631 | + |
560 | 632 | @image_comparison(baseline_images=['date_inverted_limit'],
|
561 | 633 | extensions=['png'])
|
562 | 634 | def test_date_inverted_limit():
|
@@ -701,6 +773,30 @@ def attach_tz(dt, zi):
|
701 | 773 | _test_rrulewrapper(attach_tz, pytz.timezone)
|
702 | 774 |
|
703 | 775 |
|
| 776 | +@pytest.mark.pytz |
| 777 | +@pytest.mark.skipif(not __has_pytz(), reason="Requires pytz") |
| 778 | +def test_yearlocator_pytz(): |
| 779 | + import pytz |
| 780 | + |
| 781 | + tz = pytz.timezone('America/New_York') |
| 782 | + x = [tz.localize(datetime.datetime(2010, 1, 1)) |
| 783 | + + datetime.timedelta(i) for i in range(2000)] |
| 784 | + locator = mdates.AutoDateLocator(interval_multiples=True, tz=tz) |
| 785 | + locator.create_dummy_axis() |
| 786 | + locator.set_view_interval(mdates.date2num(x[0])-1.0, |
| 787 | + mdates.date2num(x[-1])+1.0) |
| 788 | + |
| 789 | + np.testing.assert_allclose([733408.208333, 733773.208333, 734138.208333, |
| 790 | + 734503.208333, 734869.208333, |
| 791 | + 735234.208333, 735599.208333], locator()) |
| 792 | + expected = ['2009-01-01 00:00:00-05:00', |
| 793 | + '2010-01-01 00:00:00-05:00', '2011-01-01 00:00:00-05:00', |
| 794 | + '2012-01-01 00:00:00-05:00', '2013-01-01 00:00:00-05:00', |
| 795 | + '2014-01-01 00:00:00-05:00', '2015-01-01 00:00:00-05:00'] |
| 796 | + st = list(map(str, mdates.num2date(locator(), tz=tz))) |
| 797 | + assert st == expected |
| 798 | + |
| 799 | + |
704 | 800 | def test_DayLocator():
|
705 | 801 | with pytest.raises(ValueError):
|
706 | 802 | mdates.DayLocator(interval=-1)
|
|
0 commit comments