diff --git a/lib/matplotlib/dates.py b/lib/matplotlib/dates.py index 227b39945b31..417543604327 100755 --- a/lib/matplotlib/dates.py +++ b/lib/matplotlib/dates.py @@ -898,6 +898,10 @@ def get_locator(self, dmin, dmax): 'Pick the best locator based on a distance.' delta = relativedelta(dmax, dmin) + # take absolute difference + if dmin > dmax: + delta = -delta + numYears = (delta.years * 1.0) numMonths = (numYears * 12.0) + delta.months numDays = (numMonths * 31.0) + delta.days diff --git a/lib/matplotlib/tests/baseline_images/test_dates/date_inverted_limit.png b/lib/matplotlib/tests/baseline_images/test_dates/date_inverted_limit.png new file mode 100644 index 000000000000..73e635eb5189 Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_dates/date_inverted_limit.png differ diff --git a/lib/matplotlib/tests/test_coding_standards.py b/lib/matplotlib/tests/test_coding_standards.py index 3c63cd0c4a24..24bda42fba23 100644 --- a/lib/matplotlib/tests/test_coding_standards.py +++ b/lib/matplotlib/tests/test_coding_standards.py @@ -96,7 +96,6 @@ '*/matplotlib/tri/triinterpolate.py', '*/matplotlib/tests/test_axes.py', '*/matplotlib/tests/test_bbox_tight.py', - '*/matplotlib/tests/test_dates.py', '*/matplotlib/tests/test_delaunay.py', '*/matplotlib/tests/test_dviread.py', '*/matplotlib/tests/test_image.py', diff --git a/lib/matplotlib/tests/test_dates.py b/lib/matplotlib/tests/test_dates.py index e0d6415e2890..7b847bb0df6d 100644 --- a/lib/matplotlib/tests/test_dates.py +++ b/lib/matplotlib/tests/test_dates.py @@ -162,9 +162,9 @@ def test_DateFormatter(): def test_date_formatter_callable(): scale = -11 locator = mock.Mock(_get_unit=mock.Mock(return_value=scale)) - callable_formatting_function = lambda dates, _: \ - [dt.strftime('%d-%m//%Y') for dt in dates] - + callable_formatting_function = (lambda dates, _: + [dt.strftime('%d-%m//%Y') for dt in dates]) + formatter = mdates.AutoDateFormatter(locator) formatter.scaled[-10] = callable_formatting_function assert_equal(formatter([datetime.datetime(2014, 12, 25)]), @@ -223,7 +223,8 @@ def test_auto_date_locator(): def _create_auto_date_locator(date1, date2): locator = mdates.AutoDateLocator() locator.create_dummy_axis() - locator.set_view_interval(mdates.date2num(date1), mdates.date2num(date2)) + locator.set_view_interval(mdates.date2num(date1), + mdates.date2num(date2)) return locator d1 = datetime.datetime(1990, 1, 1) @@ -275,8 +276,10 @@ def _create_auto_date_locator(date1, date2): '1990-01-01 00:00:40+00:00'] ], [datetime.timedelta(microseconds=1500), - ['1989-12-31 23:59:59.999507+00:00', '1990-01-01 00:00:00+00:00', - '1990-01-01 00:00:00.000502+00:00', '1990-01-01 00:00:00.001005+00:00', + ['1989-12-31 23:59:59.999507+00:00', + '1990-01-01 00:00:00+00:00', + '1990-01-01 00:00:00.000502+00:00', + '1990-01-01 00:00:00.001005+00:00', '1990-01-01 00:00:00.001508+00:00'] ], ) @@ -288,6 +291,21 @@ def _create_auto_date_locator(date1, date2): expected) +@image_comparison(baseline_images=['date_inverted_limit'], + extensions=['png']) +def test_date_inverted_limit(): + # test ax hline with date inputs + t0 = datetime.datetime(2009, 1, 20) + tf = datetime.datetime(2009, 1, 31) + fig = plt.figure() + ax = fig.add_subplot(1, 1, 1) + ax.axhline(t0, color="blue", lw=3) + ax.set_ylim(t0 - datetime.timedelta(days=5), + tf + datetime.timedelta(days=5)) + ax.invert_yaxis() + fig.subplots_adjust(left=0.25) + + if __name__ == '__main__': import nose nose.runmodule(argv=['-s', '--with-doctest'], exit=False)