New user having much trouble with formating dates on x-axis

The script plottest.py.txt reads the data file (out.txt) and creates the plot (myfig.png); however,
I am unable to format the dates ... they always print as floats .. help

the files plottest.py.txt, out.txt and myfig.png are attached

Thanks

Andy

plottest.py.txt (924 Bytes)

out.txt (278 Bytes)

myfig.png

are those unix timestamps (from 1970-01-01)?

In any case, you have to convert those in datetime objects, then

dates = list of datetime objects
mpl_dates = [matplotlib.dates.date2num(date) for date in dates]

and at the and use plot_date() instead of plot(), using mpl_dates for X axis.

Regards,

···

On Sat, Apr 18, 2009 at 00:22, Andrew Romero <romeroajr@...9...> wrote:

The script plottest.py.txt reads the data file (out.txt) and creates the plot (myfig.png); however,
I am unable to format the dates ... they always print as floats .. help

--
Sandro Tosi (aka morph, morpheus, matrixhasu)
My website: http://matrixhasu.altervista.org/
Me at Debian: http://wiki.debian.org/SandroTosi

You can use the csv2rec function with a converter function to convert your timestamps to datetime instances. Here is an example:

import datetime
import numpy as np
import matplotlib.mlab as mlab

import matplotlib.dates as mdates
import matplotlib.pyplot as plt

def todate(s):
    'convert a unix timestamp string to a datetime instance'
    return datetime.datetime.fromtimestamp(float(s))

r = mlab.csv2rec('out.txt', names='date,val1,val2,val3,val4',
                 delimiter=' ',  converterd={'date':todate})

fig = plt.figure()
ax = fig.add_subplot(111)

ax.plot(r.date, r.val1, label='val1')
ax.plot(r.date, r.val2, label='val2')
ax.plot(r.date, r.val3, label='val3')

ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))

ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d %H:%M:%S')

leg = ax.legend(fancybox=True, shadow=True)
leg.get_frame().set_alpha(0.5)

fig.autofmt_xdate()

plt.show()

JDH

···

On Fri, Apr 17, 2009 at 5:22 PM, Andrew Romero <romeroajr@…9…> wrote:

The script plottest.py.txt reads the data file (out.txt) and creates the plot (myfig.png); however,

I am unable to format the dates … they always print as floats … help

the files plottest.py.txt, out.txt and myfig.png are attached