Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit be0aafb

Browse files
committed
Updated @megies' PR to include a test and some other tweaks.
1 parent ff76552 commit be0aafb

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

lib/matplotlib/dates.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,9 @@ class AutoDateFormatter(ticker.Formatter):
539539

540540
def __init__(self, locator, tz=None, defaultfmt='%Y-%m-%d'):
541541
"""
542-
Autofmt the date labels. The default format is the one to use
543-
if none of the times in scaled match
542+
Autoformat the date labels. The default format is the one to use
543+
if none of the values in ``self.scaled`` are greater than the unit
544+
returned by ``locator._get_unit()``.
544545
"""
545546
self._locator = locator
546547
self._tz = tz
@@ -553,21 +554,24 @@ def __init__(self, locator, tz=None, defaultfmt='%Y-%m-%d'):
553554
1. / (24. * 60.): '%H:%M:%S.%f'}
554555

555556
def __call__(self, x, pos=None):
556-
scale = float(self._locator._get_unit())
557+
locator_unit_scale = float(self._locator._get_unit())
557558
fmt = self.defaultfmt
558559

559-
for k in sorted(self.scaled):
560-
if k >= scale:
561-
fmt = self.scaled[k]
560+
# Pick the first scale which is greater than the locator unit.
561+
for possible_scale in sorted(self.scaled):
562+
if possible_scale >= locator_unit_scale:
563+
fmt = self.scaled[possible_scale]
562564
break
563565

564566
if isinstance(fmt, six.string_types):
565567
self._formatter = DateFormatter(fmt, self._tz)
566-
return self._formatter(x, pos)
568+
result = self._formatter(x, pos)
567569
elif six.callable(fmt):
568-
return fmt(x, pos)
570+
result = fmt(x, pos)
569571
else:
570-
raise NotImplementedError()
572+
raise TypeError('Unexpected type passed to {!r}.'.formatter(self))
573+
574+
return result
571575

572576

573577
class rrulewrapper:

lib/matplotlib/tests/test_dates.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import warnings
99
import tempfile
1010

11-
from nose.tools import assert_raises, assert_equal
1211
import dateutil
12+
import mock
13+
from nose.tools import assert_raises, assert_equal
1314

1415
from matplotlib.testing.decorators import image_comparison, cleanup
1516
import matplotlib.pyplot as plt
1617
import matplotlib.dates as mdates
17-
from matplotlib.dates import DayLocator
1818

1919

2020
@image_comparison(baseline_images=['date_empty'], extensions=['png'])
@@ -155,6 +155,18 @@ def test_DateFormatter():
155155
fig.autofmt_xdate()
156156

157157

158+
def test_date_formatter_callable():
159+
scale = -11
160+
locator = mock.Mock(_get_unit=mock.Mock(return_value=scale))
161+
callable_formatting_function = lambda dates, _: \
162+
[dt.strftime('%d-%m//%Y') for dt in dates]
163+
164+
formatter = mdates.AutoDateFormatter(locator)
165+
formatter.scaled[-10] = callable_formatting_function
166+
assert_equal(formatter([datetime.datetime(2014, 12, 25)]),
167+
['25-12//2014'])
168+
169+
158170
def test_drange():
159171
"""
160172
This test should check if drange works as expected, and if all the

0 commit comments

Comments
 (0)