question on usage of DateFormatter

Hello,
i am trying to plot information which will have on the x a timestamp imformation;
even if i specify the formatter i want(see code below) , it seems to be ingnored in the
labels, meaning, i don't see the time portion of it, just the date.
The code is very simple since i am just testing; thanks for suggestions in what
i am doing wrong, best regards

Margherita

···

----------------------------------------------------------
from pylab import *
from pylab import figure, show, datestr2num, load
from matplotlib.dates import DateFormatter
import datetime

id_list=[1,2,3,4,5,6]
str_dates=['2006-07-29 11:01:01','2006-07-29 10:02:03','2006-07-31 00:00:00',
          '2006-08-01 10:11:12','2006-08-02 09:09:09','2006-08-03 08:08:08']

id_dates=datestr2num(str_dates)

# It appears to ignore the formatting, it shows the month,day and year but not the time...
formatter = DateFormatter('%m-%d-%y %H:%M:%S')

ax = subplot(111)
ax.plot_date(id_dates,id_list)
ax.xaxis.set_major_formatter(dateFormatter)
labels = ax.get_xticklabels()

setp(labels, rotation=30, fontsize=8)
ax.set_title(" test dates")
show()

"dateFormatter" is not defined -- I think you mean "formatter"

When I run this example with this change, I get tick labels with the
hour, minute and second as indicated...

Note recent versions of maptlotlib can takes dates explicitly (it will
handle the conversion for you). I would write this example as
(fig.autofmt_xdate will handle the ticklabel rotation for you)

import datetime
import dateutil.parser
import matplotlib.dates as mpldates
import matplotlib.pyplot as plt

ids = [1,2,3,4,5,6]
str_dates = ['2006-07-29 11:01:01','2006-07-29 10:02:03','2006-07-31 00:00:00',
             '2006-08-01 10:11:12','2006-08-02 09:09:09','2006-08-03 08:08:08']

dates = [dateutil.parser.parse(s) for s in str_dates]

formatter = mpldates.DateFormatter('%m-%d-%y %H:%M:%S')

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(dates, ids, 'o')
ax.xaxis.set_major_formatter(formatter)

fig.autofmt_xdate()
ax.set_title("test dates")
plt.show()

JDH

···

On Tue, May 27, 2008 at 2:14 PM, Margherita Vittone wiersma <vittone@...1432...> wrote:

id_list=[1,2,3,4,5,6]
str_dates=['2006-07-29 11:01:01','2006-07-29 10:02:03','2006-07-31 00:00:00',
         '2006-08-01 10:11:12','2006-08-02 09:09:09','2006-08-03 08:08:08']

id_dates=datestr2num(str_dates)

# It appears to ignore the formatting, it shows the month,day and year but not the time...
formatter = DateFormatter('%m-%d-%y %H:%M:%S')

ax = subplot(111)
ax.plot_date(id_dates,id_list)
ax.xaxis.set_major_formatter(dateFormatter)

Hello,
thank for your reply; i made a typo when i cut and paste my example which was
longer and i just trimmed it down to email it. So even with the correct formatter defined,
on the plot i only see 00:00:00 for the time portion of the label; i am wondering
if it could related to the version of matplotlib i am using:

matplotlib-0.87.7-py2.5

thanks again

Margherita

Well that is a different problem. In your original post you wrote "I
don't see the time portion of it, just the date" so I misunderstood
your problem. With 00:00:00, you are seing the time portion of the
*tick locations*. As I wrote in a previous post, you are seeing the
format strings for the locations of the ticks, not the data points.
The default tick locator tries to put the ticks on even days. If you
want the ticks to be on the data points, you need to do:

   ax.set_xticks(dates)

to put ticks explicitly where you want them.

JDH

···

On Wed, May 28, 2008 at 10:17 AM, Margherita Vittone wiersma <vittone@...1432...> wrote:

thank for your reply; i made a typo when i cut and paste my example which was
longer and i just trimmed it down to email it. So even with the correct formatter defined,
on the plot i only see 00:00:00 for the time portion of the label