mpl.dates.AutoDateFormatter

class mpl.dates.AutoDateFormatter(locator, tz=None, defaultfmt=u'%Y-%m-%d')[source]

This class attempts to figure out the best format to use. This is most useful when used with the AutoDateLocator.

The AutoDateFormatter has a scale dictionary that maps the scale of the tick (the distance in days between one major tick) and a format string. The default looks like this:

self.scaled = {
   365.0  : '%Y',
   30.    : '%b %Y',
   1.0    : '%b %d %Y',
   1./24. : '%H:%M:%S',
   1. / (24. * 60.): '%H:%M:%S.%f',
   }

The algorithm picks the key in the dictionary that is >= the current scale and uses that format string. You can customize this dictionary by doing:

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

A custom FuncFormatter 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)
__init__(locator, tz=None, defaultfmt=u'%Y-%m-%d')[source]

Autoformat the date labels. The default format is the one to use if none of the values in self.scaled are greater than the unit returned by locator._get_unit().

Methods

__init__(locator[, tz, defaultfmt]) Autoformat the date labels.
create_dummy_axis(**kwargs)
fix_minus(s) Some classes may want to replace a hyphen for minus with the proper unicode symbol (U+2212) for typographical correctness.
format_data(value)
format_data_short(value) return a short string version
get_offset()
set_axis(axis)
set_bounds(vmin, vmax)
set_data_interval(vmin, vmax)
set_locs(locs)
set_view_interval(vmin, vmax)

Attributes

axis
locs