Skip to content

Commit 2c13f9f

Browse files
committed
FIX: check if formatters changed
1 parent 211fc06 commit 2c13f9f

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

lib/matplotlib/dates.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,10 @@ def __init__(self, locator, tz=None, formats=None, offset_formats=None,
826826
self._tz = tz
827827
self._oldticks = np.array([])
828828
self._oldlabels = None
829+
self._old_formats = [''*6]
830+
self._old_zero_formats = [''*6]
831+
self._old_offset_formats = [''*6]
832+
829833
self.defaultfmt = '%Y'
830834
# there are 6 levels with each level getting a specific format
831835
# 0: mostly years, 1: months, 2: days,
@@ -876,15 +880,27 @@ def __init__(self, locator, tz=None, formats=None, offset_formats=None,
876880
self._formatter = DateFormatter(self.defaultfmt, self._tz)
877881
self.show_offset = show_offset
878882

883+
def _anything_changed(self, ticks):
884+
""" This tests if we need to reformat the ticks..."""
885+
changed = False
886+
if (not np.array_equal(ticks, self._oldticks) or
887+
(self.formats != self._old_formats) or
888+
(self.zero_formats != self._old_zero_formats) or
889+
(self.offset_formats != self._old_offset_formats)):
890+
changed = True
891+
self._old_zero_formats = self.zero_formats.copy()
892+
self._old_offset_formats = self.offset_formats.copy()
893+
self._old_formats = self.formats.copy()
894+
return changed
895+
879896
def __call__(self, x, pos=None):
880897
if hasattr(self._locator, '_get_unit'):
881898
locator_unit_scale = float(self._locator._get_unit())
882899
else:
883900
locator_unit_scale = 1.0
884901
ticks = self._locator()
885902
if pos is not None:
886-
if not np.array_equal(ticks, self._oldticks):
887-
903+
if self._anything_changed(ticks):
888904
offset_fmt = ''
889905
fmt = self.defaultfmt
890906
self._formatter = DateFormatter(fmt, self._tz)

lib/matplotlib/tests/test_dates.py

+21-3
Original file line numberDiff line numberDiff line change
@@ -547,16 +547,34 @@ def _create_auto_date_locator(date1, date2):
547547
[datetime.timedelta(seconds=40),
548548
['00:00', '05', '10', '15', '20', '25', '30', '35', '40']
549549
],
550-
[datetime.timedelta(seconds=2),
551-
['59.5', '00:00', '00.5', '01.0', '01.5', '02.0', '02.5']
552-
],
553550
)
554551
for t_delta, expected in results:
555552
d2 = d1 + t_delta
556553
strings = _create_auto_date_locator(d1, d2)
557554
assert strings == expected
558555

559556

557+
def test_concise_formatter_change_format():
558+
fig, ax = plt.subplots()
559+
560+
locator = mdates.AutoDateLocator(interval_multiples=True)
561+
formatter = mdates.ConciseDateFormatter(locator)
562+
d1 = datetime.datetime(1997, 1, 1)
563+
d2 = d1 + datetime.timedelta(weeks=52 * 200)
564+
565+
ax.yaxis.set_major_locator(locator)
566+
ax.yaxis.set_major_formatter(formatter)
567+
ax.set_ylim(d1, d2)
568+
fig.canvas.draw()
569+
formatter.formats[0] = '%y'
570+
ax.set_ylim(d1, d2)
571+
fig.canvas.draw()
572+
sts = []
573+
for st in ax.get_yticklabels():
574+
sts += [st.get_text()]
575+
assert sts == ['80', '00', '20', '40', '60', '80',
576+
'00', '20', '40', '60', '80', '00']
577+
560578
@image_comparison(baseline_images=['date_inverted_limit'],
561579
extensions=['png'])
562580
def test_date_inverted_limit():

0 commit comments

Comments
 (0)