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

Skip to content

Commit 9e8be5b

Browse files
authored
Merge pull request #12470 from ImportanceOfBeingErnest/updateformatterwithlocator
Update AutoDateFormatter with locator
2 parents 27f9f3c + 5dffdd1 commit 9e8be5b

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

lib/matplotlib/axis.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,10 +1578,12 @@ def set_major_locator(self, locator):
15781578
locator : ~matplotlib.ticker.Locator
15791579
"""
15801580
if not isinstance(locator, mticker.Locator):
1581-
raise TypeError("formatter argument should be instance of "
1581+
raise TypeError("locator argument should be instance of "
15821582
"matplotlib.ticker.Locator")
15831583
self.isDefault_majloc = False
15841584
self.major.locator = locator
1585+
if self.major.formatter:
1586+
self.major.formatter._set_locator(locator)
15851587
locator.set_axis(self)
15861588
self.stale = True
15871589

@@ -1594,10 +1596,12 @@ def set_minor_locator(self, locator):
15941596
locator : ~matplotlib.ticker.Locator
15951597
"""
15961598
if not isinstance(locator, mticker.Locator):
1597-
raise TypeError("formatter argument should be instance of "
1599+
raise TypeError("locator argument should be instance of "
15981600
"matplotlib.ticker.Locator")
15991601
self.isDefault_minloc = False
16001602
self.minor.locator = locator
1603+
if self.minor.formatter:
1604+
self.minor.formatter._set_locator(locator)
16011605
locator.set_axis(self)
16021606
self.stale = True
16031607

lib/matplotlib/dates.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,8 +820,14 @@ def __init__(self, locator, tz=None, defaultfmt='%Y-%m-%d'):
820820
1. / (MUSECONDS_PER_DAY):
821821
rcParams['date.autoformatter.microsecond']}
822822

823+
def _set_locator(self, locator):
824+
self._locator = locator
825+
823826
def __call__(self, x, pos=None):
824-
locator_unit_scale = float(self._locator._get_unit())
827+
try:
828+
locator_unit_scale = float(self._locator._get_unit())
829+
except AttributeError:
830+
locator_unit_scale = 1
825831
# Pick the first scale which is greater than the locator unit.
826832
fmt = next((fmt for scale, fmt in sorted(self.scaled.items())
827833
if scale >= locator_unit_scale),

lib/matplotlib/tests/test_dates.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import matplotlib.pyplot as plt
1212
from matplotlib.cbook import MatplotlibDeprecationWarning
1313
import matplotlib.dates as mdates
14+
import matplotlib.ticker as mticker
1415

1516

1617
def __has_pytz():
@@ -220,6 +221,38 @@ def test_DateFormatter():
220221
fig.autofmt_xdate()
221222

222223

224+
def test_locator_set_formatter():
225+
"""
226+
Test if setting the locator only will update the AutoDateFormatter to use
227+
the new locator.
228+
"""
229+
plt.rcParams["date.autoformatter.minute"] = "%d %H:%M"
230+
t = [datetime.datetime(2018, 9, 30, 8, 0),
231+
datetime.datetime(2018, 9, 30, 8, 59),
232+
datetime.datetime(2018, 9, 30, 10, 30)]
233+
x = [2, 3, 1]
234+
235+
fig, ax = plt.subplots()
236+
ax.plot(t, x)
237+
ax.xaxis.set_major_locator(mdates.MinuteLocator((0, 30)))
238+
fig.canvas.draw()
239+
ticklabels = [tl.get_text() for tl in ax.get_xticklabels()]
240+
expected = ['30 08:00', '30 08:30', '30 09:00',
241+
'30 09:30', '30 10:00', '30 10:30']
242+
assert ticklabels == expected
243+
244+
ax.xaxis.set_major_locator(mticker.NullLocator())
245+
ax.xaxis.set_minor_locator(mdates.MinuteLocator((5, 55)))
246+
decoy_loc = mdates.MinuteLocator((12, 27))
247+
ax.xaxis.set_minor_formatter(mdates.AutoDateFormatter(decoy_loc))
248+
249+
ax.xaxis.set_minor_locator(mdates.MinuteLocator((15, 45)))
250+
fig.canvas.draw()
251+
ticklabels = [tl.get_text() for tl in ax.get_xticklabels(which="minor")]
252+
expected = ['30 08:15', '30 08:45', '30 09:15', '30 09:45', '30 10:15']
253+
assert ticklabels == expected
254+
255+
223256
def test_date_formatter_strftime():
224257
"""
225258
Tests that DateFormatter matches datetime.strftime,

lib/matplotlib/ticker.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ def fix_minus(self, s):
299299
"""
300300
return s
301301

302+
def _set_locator(self, locator):
303+
""" Subclasses may want to override this to set a locator. """
304+
pass
305+
302306

303307
class IndexFormatter(Formatter):
304308
"""

0 commit comments

Comments
 (0)