basic understanding of plotting dates

This is really basic stuff but I had some problems navigating in the matplotlib website (*if anyone is interested, I’ll list those issues at the end). I want to simply plot dates. After reading the tutorial, I just don’t understand how to do it.

I will have lists of dates in the format like 2007-09-01 12:00:02 and a list of corresponding values. I would like to make a simple line graph with markers at the data points and axis markers at reasonable intervals such as each day or each week. My questions are:

  1. What exactly must I import (which modules) and how do I import them (in the sense of “import x” vs. “from x import y”)?

  2. What arguments does the plot_date() command take and what is format of the arguments?

  3. Do I have to make the conversion from the date format above to the matplotlib date format? If so, how?

As an example how would I make a line plot with markers for this data?:

x = (2007-09-01 12:00:02, 2007-09-02 12:00:02, 2007-09-03 12:00:02)

y = (10, 20, 30)

Thanks.

···

*the issues I had with navigating or figuring this out on my own:

Broken links on two of the functions:
http://matplotlib.sourceforge.net/datetime.html

http://matplotlib.sourceforge.net/dateutil.html

In the tutorial, under the dates section, the drange command is used but it isn’t clear which module has to be imported for it to work. I tried it with matplotlib and Python’s datetime module and it didn’t work.

In the date_demo1.py and dat_demo2 in the examples, date is gotten from the quotes_historical_yahoo module calling up Yahoo’s stock quotes. The problem for me was I wasn’t sure which format the arguments were in that this quotes_historical_yahoo() returns.

C M wrote:

1. What exactly must I import (which modules) and how do I import them
(in the sense of "import x" vs. "from x import y")?
2. What arguments does the plot_date() command take and what is format
of the arguments?
3. Do I have to make the conversion from the date format above to the
matplotlib date format? If so, how?

<<

The code snippet below should answer most of your questions.

2 problems:

autofmt_xdate() did not rotate the minor tick label, I don't now how to
do that.

I could not figure out how to draw minor tick grid lines.

import datetime as DT
import pylab as P
import time

x = ['2007-09-01 12:00:02', '2007-09-02 12:00:02', '2007-09-03 12:00:02']
y = [10, 20, 30]
fmt='%Y-%m-%d %H:%M:%S'
x1=[DT.datetime(*time.strptime(d,fmt)[:6]) for d in x]
dates=P.date2num(x1)
fig = P.figure()
ax = fig.add_subplot(111)
ax.plot_date(dates, y)
ax.xaxis.set_major_locator( P.DayLocator() )
ax.xaxis.set_minor_locator( P.HourLocator(12))
ax.xaxis.set_major_formatter( P.DateFormatter('%Y-%m-%d') )
ax.xaxis.set_minor_formatter( P.DateFormatter('%Y-%m-%d %H:%M:%S'))
ax.grid(True)
fig.autofmt_xdate()
P.show()

···

--
Bill

wjdandreta@...872...

Gentoo Linux X86_64 2.6.20-gentoo-r8

Reclaim Your Inbox with http://www.mozilla.org/products/thunderbird/

All things cometh to he who waiteth as long as he who waiteth worketh like hell while he waiteth.

Bill Dandreta wrote:

C M wrote:

1. What exactly must I import (which modules) and how do I import them
(in the sense of "import x" vs. "from x import y")?
2. What arguments does the plot_date() command take and what is format
of the arguments?
3. Do I have to make the conversion from the date format above to the
matplotlib date format? If so, how?

<<

The code snippet below should answer most of your questions.

  Incidentally, is there a reason why matplotlib can't just handle datetime objects itself? The requirement of having to manually convert them to an ad-hoc matplotlib "format" (which is just an integer) seems rather obtuse.

···

--
--Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is no path, and leave a trail."
  --author unknown

It can handle native datetime objects, as of recent versions, but it
is not trivial. The original versions of matplotlib assumed you were
passing in floating point (not integer) sequences (matplotlib was
written before python had a datetime object by the way, and we
supported conversion from mx datetime objects). So we supplied some
conversion functions to convert mx dates or python datetimes to
floating point numbers so matplotlib could handle them like all other
numbers, and used custom tick locators and tick formatters decorate
the date axes.

More recent versions of matplotlib support plotting with custom (non
scalar) types via a conversion registry, so you can do the obvious
thing with native datetime objects.

JDH

···

On 9/4/07, Brendan Barnwell <brenbarn@...1219...> wrote:

        Incidentally, is there a reason why matplotlib can't just handle datetime
objects itself? The requirement of having to manually convert them to an ad-hoc
matplotlib "format" (which is just an integer) seems rather obtuse.