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

Skip to content

Update AutoDateFormatter with locator #12470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1579,10 +1579,12 @@ def set_major_locator(self, locator):
locator : ~matplotlib.ticker.Locator
"""
if not isinstance(locator, mticker.Locator):
raise TypeError("formatter argument should be instance of "
raise TypeError("locator argument should be instance of "
"matplotlib.ticker.Locator")
self.isDefault_majloc = False
self.major.locator = locator
if self.major.formatter:
self.major.formatter._set_locator(locator)
locator.set_axis(self)
self.stale = True

Expand All @@ -1595,10 +1597,12 @@ def set_minor_locator(self, locator):
locator : ~matplotlib.ticker.Locator
"""
if not isinstance(locator, mticker.Locator):
raise TypeError("formatter argument should be instance of "
raise TypeError("locator argument should be instance of "
"matplotlib.ticker.Locator")
self.isDefault_minloc = False
self.minor.locator = locator
if self.minor.formatter:
self.minor.formatter._set_locator(locator)
locator.set_axis(self)
self.stale = True

Expand Down
8 changes: 7 additions & 1 deletion lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,8 +820,14 @@ def __init__(self, locator, tz=None, defaultfmt='%Y-%m-%d'):
1. / (MUSECONDS_PER_DAY):
rcParams['date.autoformatter.microsecond']}

def _set_locator(self, locator):
self._locator = locator

def __call__(self, x, pos=None):
locator_unit_scale = float(self._locator._get_unit())
try:
locator_unit_scale = float(self._locator._get_unit())
except AttributeError:
locator_unit_scale = 1
# Pick the first scale which is greater than the locator unit.
fmt = next((fmt for scale, fmt in sorted(self.scaled.items())
if scale >= locator_unit_scale),
Expand Down
33 changes: 33 additions & 0 deletions lib/matplotlib/tests/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import matplotlib.pyplot as plt
from matplotlib.cbook import MatplotlibDeprecationWarning
import matplotlib.dates as mdates
import matplotlib.ticker as mticker


def __has_pytz():
Expand Down Expand Up @@ -220,6 +221,38 @@ def test_DateFormatter():
fig.autofmt_xdate()


def test_locator_set_formatter():
"""
Test if setting the locator only will update the AutoDateFormatter to use
the new locator.
"""
plt.rcParams["date.autoformatter.minute"] = "%d %H:%M"
t = [datetime.datetime(2018, 9, 30, 8, 0),
datetime.datetime(2018, 9, 30, 8, 59),
datetime.datetime(2018, 9, 30, 10, 30)]
x = [2, 3, 1]

fig, ax = plt.subplots()
ax.plot(t, x)
ax.xaxis.set_major_locator(mdates.MinuteLocator((0, 30)))
fig.canvas.draw()
ticklabels = [tl.get_text() for tl in ax.get_xticklabels()]
expected = ['30 08:00', '30 08:30', '30 09:00',
'30 09:30', '30 10:00', '30 10:30']
assert ticklabels == expected

ax.xaxis.set_major_locator(mticker.NullLocator())
ax.xaxis.set_minor_locator(mdates.MinuteLocator((5, 55)))
decoy_loc = mdates.MinuteLocator((12, 27))
ax.xaxis.set_minor_formatter(mdates.AutoDateFormatter(decoy_loc))

ax.xaxis.set_minor_locator(mdates.MinuteLocator((15, 45)))
fig.canvas.draw()
ticklabels = [tl.get_text() for tl in ax.get_xticklabels(which="minor")]
expected = ['30 08:15', '30 08:45', '30 09:15', '30 09:45', '30 10:15']
assert ticklabels == expected


def test_date_formatter_strftime():
"""
Tests that DateFormatter matches datetime.strftime,
Expand Down
4 changes: 4 additions & 0 deletions lib/matplotlib/ticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ def fix_minus(self, s):
"""
return s

def _set_locator(self, locator):
""" Subclasses may want to override this to set a locator. """
pass


class IndexFormatter(Formatter):
"""
Expand Down