@header@
matplotlib.dates | index /usr/local/lib/python2.3/site-packages/matplotlib/dates.py |
The dates module is built around the assumption that users will want
to specify dates in a variety of ways.
The goal of the converter classes defined here is to hide all the
conversions required to plot data on the screen from you, and to
provide an abstract interface to matplotlib plotting routines so you
can use date instances of your choice. You pass these converter
classes to date plotting functions and these functions will do the
rest.
The following converters are provided
* EpochConverter - dates are seconds since the epoch
* PyDatetimeConverter - dates are python2.3 date/datetime instances
* MxDatetimeConverter - dates are egenix mx.Datetime instances
You can provide your own converter by deriving from the base class
DateConverter and overriding the methods epoch and from_epoch. If you
define one for a widely used date class, or extend the ones here,
please contribute the code back! All conversions are done to and from
seconds since the epoch locatime.
Here is an example of how you could plot dates with python datetime
instances in the eastern time zone::
from matplotlib.dates import PyDatetimeConverter, Eastern
from matplotlib.matlab import *
dates = [ dt1, dt2, dt2, ...] # the datetime instances
vals = [ val1, val2, val3, ...] # the y values
converter = PyDatetimeConverter(Eastern) # dates are in Eastern tz
ax = subplot(111)
plot_date(dates, vals, converter)
The matplotlib.ticker module provides many custom tick locators and
formatters for you so you don't have to work at the level of
manipulating epoch seconds to set your ticks. Eg, to set the ticks on
the years, you would do::
from matplotlib.ticker import YearLocator
... your plot here ...
years = YearLocator(5) # every 5 years
ax.xaxis.set_major_locator(years)
and the date tick locator will try and pick the best axis range and
tick locations for you, ie, on multiples of 5 years. Although the
default choices will probably be good, if you are unhappy with the
range choices the locator makes for you, you can use your date
instances and converter to specify them yourself, as in::
dmin = converter( datetime(2002,01,15) )
dmax = converter( datetime(2004,01,15) )
ax.set_xlim( (dmin, dmax) )
Use date formatters to specify major and minor tick formats. Minor
ticks are off by default, but you can turn them on by passing a date
locator and date formatter to the axis. The relevant methods are::
ax.xaxis.set_major_locator( majLocator )
ax.xaxis.set_major_formatter( majFormatter )
ax.xaxis.set_minor_locator( minLocator )
ax.xaxis.set_minor_formatter( minFormatter )
where majLocator and minLocator are matplotlib.ticker.Locator instance
and majFormatter and minFormatter are matplotlib.ticker.Formatter
instances. Many date locators are provided: MinuteLocator,
HourLocator, DayLocator, WeekdayLocator, MonthLocator, YearLocator.
Most of these take an argument to, for example, select only hours or
years that are multiples of some number. The DateFormatter class
takes a strftime format string to format the dates on the major and
minor ticker. The minor ticker is off by default in all plots so you
will need to set the minor locator if you want to turn them on, and
the minor formatter if you want to label them.
Here is an example setting the major date formatter
strftime string, as in::
formatter = DateFormatter('%Y')
ax.xaxis.set_major_formatter(formatter)
See the examples/dates*.py in the matplotlib src distribution for
complete demos, and the matplotlib.ticker help for more information on
tick locators and formatters.
Modules | ||||||
|
Classes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Functions | ||
Data | ||
Central = Central DSTDIFF = datetime.timedelta(0, 3600) DSTEND = datetime.datetime(1, 10, 25, 1, 0) DSTOFFSET = datetime.timedelta(-1, 68400) DSTSTART = datetime.datetime(1, 4, 1, 2, 0) Eastern = Eastern FRIDAY = 4 HOUR = datetime.timedelta(0, 3600) Local = <matplotlib.dates.LocalTimezone object> MONDAY = 0 Mountain = Mountain Pacific = Pacific SATURDAY = 5 SEC_PER_DAY = 86400 SEC_PER_HOUR = 3600 SEC_PER_MIN = 60 SEC_PER_WEEK = 604800 STDOFFSET = datetime.timedelta(-1, 64800) SUNDAY = 6 THURSDAY = 3 TUESDAY = 1 WEDNESDAY = 2 WEEKDAYS = (0, 1, 2, 3, 4, 5, 6) ZERO = datetime.timedelta(0) __warningregistry__ = {('integer multiplication', <class exceptions.OverflowWarning>, 158): 1, ('integer multiplication', <class exceptions.OverflowWarning>, 160): 1} division = _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192) utc = <matplotlib.dates.UTC object> |