8000 Update AutoDateFormatter with locator · matplotlib/matplotlib@9eda3db · GitHub
[go: up one dir, main page]

Skip to content

Commit 9eda3db

Browse files
Update AutoDateFormatter with locator
1 parent 74e0665 commit 9eda3db

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

lib/matplotlib/axis.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,10 +1579,13 @@ def set_major_locator(self, locator):
15791579
locator : ~matplotlib.ticker.Locator
15801580
"""
15811581
if not isinstance(locator, mticker.Locator):
1582-
raise TypeError("formatter argument should be instance of "
1582+
raise TypeError("locator argument should be instance of "
15831583
"matplotlib.ticker.Locator")
15841584
self.isDefault_majloc = False
15851585
self.major.locator = locator
1586+
if (hasattr(self.get_major_formatter(), "_locator") and
1587+
hasattr(self.get_major_formatter(), "_tz")):
1588+
self.get_major_formatter()._locator = locator
15861589
locator.set_axis(self)
15871590
self.stale = True
15881591

@@ -1595,10 +1598,13 @@ def set_minor_locator(self, locator):
15951598
locator : ~matplotlib.ticker.Locator
15961599
"""
15971600
if not isinstance(locator, mticker.Locator):
1598-
raise TypeError("formatter argument should be instance of "
1601+
raise TypeError("locator argument should be instance of "
15991602
"matplotlib.ticker.Locator")
16001603
self.isDefault_minloc = False
16011604
self.minor.locator = locator
1605+
if (hasattr(self.get_minor_formatter(), "_locator") and
1606+
hasattr(self.get_minor_formatter(), "_tz")):
1607+
self.get_minor_formatter()._locator = locator
16021608
locator.set_axis(self)
16031609
self.stale = True
16041610

lib/matplotlib/tests/test_dates.py

Lines changed: 21 additions & 0 deletions
Original file line num D8AE berDiff line numberDiff line change
@@ -220,6 +220,27 @@ def test_DateFormatter():
220220
fig.autofmt_xdate()
221221

222222

223+
def test_locator_set_formatter():
224+
"""
225+
Test if setting the locator only will update the formatter to use
226+
the new locator.
227+
"""
228+
plt.rcParams["date.autoformatter.minute"] = "%d %H:%M"
229+
t = [datetime.datetime(2018, 9, 30, 8, 0),
230+
datetime.datetime(2018, 9, 30, 8, 59),
231+
datetime.datetime(2018, 9, 30, 10, 30)]
232+
x = [2, 3, 1]
233+
234+
fig, ax = plt.subplots()
235+
ax.plot(t, x)
236+
ax.xaxis.set_major_locator(mdates.MinuteLocator((0, 30)))
237+
fig.canvas.draw()
238+
ticklabels = [tl.get_text() for tl in ax.get_xticklabels()]
239+
expected = ['30 08:00', '30 08:30', '30 09:00',
240+
'30 09:30', '30 10:00', '30 10:30']
241+
assert ticklabels == expected
242+
243+
223244
def test_date_formatter_strftime():
224245
"""
225246
Tests that DateFormatter matches datetime.strftime,

0 commit comments

Comments
 (0)
0