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

Skip to content

Commit ddb55d2

Browse files
Update AutoDateFormatter with locator
1 parent 74e0665 commit ddb55d2

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,26 @@ 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), datetime.datetime(2018,9,30,8,59),
230+
datetime.datetime(2018,9,30,10,30)]
231+
x = [2,3,1]
232+
233+
fig, ax = plt.subplots()
234+
ax.plot(t,x)
235+
ax.xaxis.set_major_locator(mdates.MinuteLocator((0,30)))
236+
fig.canvas.draw()
237+
ticklabels = [tl.get_text() for tl in ax.get_xticklabels()]
238+
expected = ['30 08:00', '30 08:30', '30 09:00',
239+
'30 09:30', '30 10:00', '30 10:30']
240+
assert ticklabels == expected
241+
242+
223243
def test_date_formatter_strftime():
224244
"""
225245
Tests that DateFormatter matches datetime.strftime,

0 commit comments

Comments
 (0)
0