plotting data against a time axis

John Hunter jdhunter at ace.bsd.uchicago.edu
Sun Nov 16 01:01:29 CET 2003


>>>>> "Gerrit" == Gerrit Holl <gerrit at nl.linux.org> writes:

    Gerrit> Hi, I have a dictionairy containing DateTime objects as
    Gerrit> keys and integers as values. What would be the easiest way
    Gerrit> to create a simple plot of these, with a number axis
    Gerrit> versus a time axis? What library is the most suitable for
    Gerrit> this? 'plot' on parnassus yields 18 hits, but since I have
    Gerrit> zero experience, I don't know where to start. What makes
    Gerrit> it difficult is that I have a time axis instead of a
    Gerrit> simple integer x-axis. Gnuplot doesn't seem to be able to
    Gerrit> do this, or does it?

There are a lot of good plotting libraries out there.  I'll pitch
mine.  http://matplotlib.sourceforge.net supports a wide range of 2D
plotting capabilities with high quality output.  It doesn't accept
datetime instances natively (as most plot libs won't), but you can
convert them to numbers and plot the numbers on the xaxis.  Then
you'll want to use the datetime format string methods to create the
ticklabels and set them manually.

Here's an example that generates a bar plot of counts versus day of
month (I'm assuming your using python2.3's datetime class, but
mx.DateTime would work the same way).

    from datetime import date
    from matplotlib.matlab import plot, show, title, \
         xlabel, ylabel, set, gca, bar, savefig

    d = {date(2003,11,1) : 12,
         date(2003,11,2) : 20,
         date(2003,11,5) : 20,
         date(2003,11,9) : 18,
         date(2003,11,12) : 2,
         }

    # create a list of key,val items so you can sort by date
    dates = [ (date, count) for date, count in d.items()]
    dates.sort()

    dates, counts = zip(*dates)          # split to two lists
    days = [date.day for date in dates]  # get the day
    bar(days, counts)                    # bar plot of days vs counts
    set(gca(), 'xticks', days)           # force the ticks to fall on the days
    xlabel('Nov day')
    ylabel('count')
    show()

You could also do 'plot' instead of 'bar'.

If you need to do fancier formatting of the tick labels, as you
probably do, you can use the set_xticklabels command to format the
ticklabels

    from datetime import date
    from matplotlib.matlab import plot, show, title, \
         xlabel, ylabel, set, gca, bar, savefig

    d = {date(2003,11,1) : 12,
         date(2003,11,2) : 20,
         date(2003,11,5) : 20,
         date(2003,11,9) : 18,
         date(2003,11,12) : 2,
         }

    dates = [ (date, count) for date, count in d.items()]
    dates.sort()
    labels = [date.strftime('%b %d') for date, count in dates]

    dates, counts = zip(*dates)  # split to two lists
    days = [date.day for date in dates]  # get the day
    plot(days, counts)
    set(gca(), 'xticks', days)  # force the ticks to fall on the days
    set(gca(), 'xticklabels', labels)  # force the ticks to fall on the days
    ylabel('count')

    show()

matplotlib currently works under GUI frameworks pygtk and wxpython,
and can work 'offline' for batch processing with postscript and
gdmodule outputs.

Other good alternatives for plotting are chaco, xplt, gnuplot, pyx,
and more.... See also
http://www.python.org/topics/scicomp/plotting.html

Hope this helps!
John Hunter





More information about the Python-list mailing list