Skip to content

AutoDateFormatter: More customizable formatting #2507

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

Closed
Closed
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
33 changes: 27 additions & 6 deletions lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,25 @@ class AutoDateFormatter(ticker.Formatter):
dictionary by doing::


formatter = AutoDateFormatter()
formatter.scaled[1/(24.*60.)] = '%M:%S' # only show min and sec

>>> formatter = AutoDateFormatter()
>>> formatter.scaled[1/(24.*60.)] = '%M:%S' # only show min and sec

Custom `FunctionFormatter`s can also be used. The following example shows
how to use a custom format function to strip trailing zeros from decimal
seconds and adds the date to the first ticklabel::

>>> def my_format_function(x, pos=None):
... x = matplotlib.dates.num2date(x)
... if pos == 0:
... fmt = '%D %H:%M:%S.%f'
... else:
... fmt = '%H:%M:%S.%f'
... label = x.strftime(fmt)
... label = label.rstrip("0")
... label = label.rstrip(".")
... return label
>>> from matplotlib.ticker import FuncFormatter
>>> formatter.scaled[1/(24.*60.)] = FuncFormatter(my_format_function)
"""

# This can be improved by providing some user-level direction on
Expand Down Expand Up @@ -536,7 +552,7 @@ def __init__(self, locator, tz=None, defaultfmt='%Y-%m-%d'):
1. / 24.: '%H:%M:%S',
1. / (24. * 60.): '%H:%M:%S.%f'}

def __call__(self, x, pos=0):
def __call__(self, x, pos=None):
scale = float(self._locator._get_unit())
fmt = self.defaultfmt

Expand All @@ -545,8 +561,13 @@ def __call__(self, x, pos=0):
fmt = self.scaled[k]
break

self._formatter = DateFormatter(fmt, self._tz)
return self._formatter(x, pos)
if isinstance(fmt, six.string_types):
self._formatter = DateFormatter(fmt, self._tz)
return self._formatter(x, pos)
elif six.callable(fmt):
return fmt(x, pos)
else:
raise NotImplementedError()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably raise a more descriptive error.



class rrulewrapper:
Expand Down