Crude but possibly useful little hack

I've been using matplotlib for some plotting involving

    > dated values, but wasn't able to figure out how to use
    > the new plot_date with log axes. (I'm still a rank newbie

Very nice; I haven't used property but I can see it is very useful
and I should be using it more....

For the record, you can set the scaling on the x axis to be
logarithmic with

  ax.set_xscale('log')

or

  set(gca(), 'xscale', 'log')

    > at matplotlib.) It struck me that an integer that "knew"
    > it represented seconds since the epoch would be a neat
    > way of feeding matplotlib's non-date functions the values
    > they expected, while at the same time manipulating dates
    > in my own code; and, since I'd just done a bit of reading
    > of new-style classes in Python, came up with the
    > following:

The other nice thing about this is that it can be used with bar,
scatter, etc. and all the tick locators and formatters still work. I
wrote a little example using bar just to convince myself of this :slight_smile:

import datetime, time
from matplotlib.ticker import MinuteLocator, DateFormatter
from matplotlib.matlab import *

class intdate(int):
     '''Subclasses int for use as dates.'''
     def __init__(self, ordinal):
         int.__init__(self, ordinal)
         self.__date = datetime.date.fromtimestamp(ordinal)

     day = property(fget=lambda self:self.__date.day)
     month = property(fget=lambda self:self.__date.month)
     year = property(fget=lambda self:self.__date.year)

     def isoformat(self): return self.__date.isoformat()

     def timetuple(self): return self.__date.timetuple()

     def date(self): return self.__date

def epoch(x):
     'convert userland datetime instance x to epoch'
     return time.mktime(x.timetuple())

def date(year, month, day):
     return intdate(epoch(datetime.date(year, month, day)))

def today():
     return intdate(epoch(datetime.date.today()))

# simulate collecting data every minute starting at midnight
t0 = date(2004,04,27)
t = t0+arange(0, 2*3600, 60) # 2 hours sampled every 2 minute
s = rand(len(t))

ax = subplot(111)
ax.xaxis.set_major_locator( MinuteLocator(20) )
ax.xaxis.set_major_formatter( DateFormatter('%H:%M') )
ax.bar(t, s, width=60)
show()

Do you mind if I include these date classes and functions in
matplotlib.dates? I would probably need to rename the functions to
avoid clashing with other namespaces, something like epoch_to_intdate,
ymd_to_intdate, today_to_intdate.

Thanks!
John Hunter