Hello, I am trying to learn how to plot using dates. In my
> code I'm basically trying to do the following:
> t1 = ['05/05/2005', '05/06/2005', '05/07/2005',
> '05/08/2005']
> t3 = [1,2,3,4]
> #plot data self.axes.plot_date(t1, t3, 'bo')
> this doesn't work for me. What does the input for plot_date
> need to look like? Thanks.
From the plot_date help
plot_date(self, d, y, fmt='bo', tz=None, **kwargs)
PLOT_DATE(d, y, converter, fmt='bo', tz=None, **kwargs)
d is a sequence of dates represented as float days since
0001-01-01 UTC and y are the y values at those dates. fmt is
a plot format string. kwargs are passed on to plot. See plot
for more information.
See matplotlib.dates for helper functions date2num, num2date
and drange for help on creating the required floating point dates
tz is the timezone - defaults to rc value
The snippet below should get you started...
from matplotlib.dates import date2num
from datetime import datetime
def ymd(s):
m,d,y = [int(num) for num in s.split('/')]
return y,m,d
t1 = ['05/05/2005', '05/06/2005', '05/07/2005', '05/08/2005']
datenums = date2num([datetime(*ymd(s)) for s in t1])
See also examples/date_demo*.py in the matplotlib src distro or zipped
up here
http://matplotlib.sourceforge.net/matplotlib_examples_0.80.zip
JDH