dates for x-axis

Hi matplotlib list,
I am having trouble applying mdates in matplotlib.
I am not sure how to recognise that x-axis are dates like 20110101, 20110102, 20110103 etc.
Any feedback will be greatly appreciated!
below is the code I have so far:

import numpy as np
import matplotlib.pyplot as plt
from numpy import ma as MA
from mpl_toolkits.basemap import Basemap
import os
import glob
import matplotlib.dates as mdates

MainFolder=r"E:/Rainfall/rainfall-2011/test/"
OutputFolder=r"E:/test_out/"

rmax=[]
monthyear=[]

for (path, dirs, files) in os.walk(MainFolder):
path=path+’/’
for fname in files:
if fname.endswith(’.txt’):
filename=path+fname
fileName, fileExtension=os.path.splitext(fname)
test=fileName.strip(‘r’)
test=str(test)
monthyear.append(test)
f=np.genfromtxt(filename, skip_header=6)
dailymax=f.max()
rmax.append(dailymax)
print rmax
print monthyear

x=monthyear
y=rmax

fig, ax=plt.subplots(1)

plt.plot(x,y)

fig.autofmt_xdate()
ax.fmt_xdata=mdates.DateFormatter(’%Y%m%d’)

plt.ylabel(“Precipitation”)
plt.title(“Max daily Precipition Vic”)
plt.savefig(OutputFolder+“MaxdailyPrecip.png”)
plt.show()

Use datetime objects instead of strings.

Goyo

···

El día 12 de abril de 2012 03:46, questions anon <questions.anon@...287...> escribió:

I am not sure how to recognise that x-axis are dates like 20110101,
20110102, 20110103 etc.

I am not exactly sure how to use datetime objects instead of strings.
This is the code I am working with at the moment and the code works except for the dates, they are just weird numbers along the x-axis.
Any help will be greatly appreciated.

import numpy as np
import matplotlib.pyplot as plt
from numpy import ma as MA
from mpl_toolkits.basemap import Basemap
from datetime import datetime
import os
from osgeo import gdal, gdalnumeric, ogr, osr
import glob
from datetime import date, timedelta
import matplotlib.dates as mdates
import time

rainmax=[]
yearmonthlist=[]
yearmonth_int=[]

OutputFolder=r"E:/test_out/"
GLOBTEMPLATE = r"e:/Rainfall/rainfall-{year}/r{year}{month:02}??.txt"

def accumulate_month(year, month):
files = glob.glob(GLOBTEMPLATE.format(year=year, month=month))
monthlyrain=[]
monthlyrainaust=[]
for ifile in files:
f=np.genfromtxt(ifile,skip_header=6)
monthlyrain.append(f)
yearmonth=str(year)+str(month)
d=datetime.strptime(yearmonth, ‘%Y%m’)
date_string=d.strftime(’%Y%m’)
yearmonthint=int(date_string)
yearmonth_int.append(yearmonthint)
yearmonthlist.append(yearmonth)
r_max=np.max(monthlyrain)
rainmax.append(r_max)

###loop through months and years
stop_month = datetime(2011, 04, 01)
month = datetime(2011, 01, 01)
while month < stop_month:
accumulate_month(month.year, month.month)
month += timedelta(days=32)
month = month.replace(day=01)

x=yearmonthlist
y=rainmax
x2=yearmonth_int

print x, y, x2

fig, ax=plt.subplots(1)

z=np.polyfit(x2,y,1)
p=np.poly1d(z)

plt.plot(x,y)
plt.plot(x,p(x2),‘r–’) #add trendline to plot
print “y=%.6fx+(%.6f)”%(z[0],z[1])

fig.autofmt_xdate()
ax.fmt_xdata=mdates.DateFormatter(’%Y%m’)
ax.xaxis.set_major_formatter(mdates.DateFormatter(’%Y%m’))

plt.xlabel(“year-month”)
plt.ylabel(“Precipitation (mm)”)
plt.title(“Max monthly Precipition”)
plt.savefig(OutputFolder+“MaxMonthlyPrecip.png”)
plt.show()

···

On Fri, Apr 13, 2012 at 2:31 AM, Goyo <goyodiaz@…287…> wrote:

El día 12 de abril de 2012 03:46, questions anon > > <questions.anon@…287…> escribió:

I am not sure how to recognise that x-axis are dates like 20110101,

20110102, 20110103 etc.

Use datetime objects instead of strings.

Goyo

Seems like you're plotting yearmonthlist in the x axis, which is a
list of strings and each string is the concatenation of the string
representations of two numbers. So numbers in the x axis are to be
expected.

You can create datetime objects this way:

d = datetime.datetime(year, month, 1)

Then create an array of datetime objects and use it as the x parameter to plot.

Goyo

···

El día 18 de abril de 2012 07:59, questions anon <questions.anon@...287...> escribió:

I am not exactly sure how to use datetime objects instead of strings.
This is the code I am working with at the moment and the code works except
for the dates, they are just weird numbers along the x-axis.

Thank you, I was able to get it to work but only if I imported datetime within the loop, otherwise I ended up with the
AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’
and if I added ‘import datetime’ at the top of my script it had an error where I loop through combining each month
" stop_month = datetime(2011, 03, 01)
TypeError: ‘module’ object is not callable"
It seems very messy with importing datetime everywhere but I am not sure what the problem is. Below is the code I am using that works:

import numpy as np
import matplotlib.pyplot as plt
from numpy import ma as MA
from mpl_toolkits.basemap import Basemap
from datetime import datetime
import os
from StringIO import StringIO
from osgeo import gdal, gdalnumeric, ogr, osr
import glob
from datetime import date, timedelta
import matplotlib.dates as mdates
import time

rainmax=[]
rainmin=[]
rainmean=[]
yearmonthlist=[]
yearmonth_int=[]

OutputFolder=r"E:/test_out/"
GLOBTEMPLATE = r"e:/Rainfall/rainfall-{year}/r{year}{month:02}??.txt"

def accumulate_month(year, month):
files = glob.glob(GLOBTEMPLATE.format(year=year, month=month))
monthlyrain=[]
for ifile in files:
f=np.genfromtxt(ifile,skip_header=6)
monthlyrain.append(f)

import datetime
yearmonth=datetime.datetime(year,month,1)
yearmonthlist.append(yearmonth)
yearmonthint=str(year)+str(month)
from datetime import date, datetime
d=datetime.strptime(yearmonthint, '%Y%m')
date_string=d.strftime('%Y%m')
yearmonthint=int(date_string)
yearmonth_int.append(yearmonthint)
r_max, r_mean, r_min=MA.max(monthlyrain), MA.mean(monthlyrain), MA.min(monthlyrain)
rainmax.append(r_max)
rainmean.append(r_mean)
rainmin.append(r_min)

###loop through months and years
stop_month = datetime(2011, 12, 31)
month = datetime(2011, 01, 01)
while month < stop_month:
accumulate_month(month.year, month.month)
month += timedelta(days=32)
month = month.replace(day=01)

Plot timeseries of max data

x=yearmonthlist
y=rainmax
x2=yearmonth_int

print x, y, x2

fig, ax=plt.subplots(1)

z=np.polyfit(x2,y,1)
p=np.poly1d(z)

plt.plot(x,y)
plt.plot(x,p(x2),‘r–’) #add trendline to plot
print “y=%.6fx+(%.6f)”%(z[0],z[1])

fig.autofmt_xdate()
ax.fmt_xdata=mdates.DateFormatter(’%Y%m’)
ax.xaxis.set_major_formatter(mdates.DateFormatter(’%Y%m’))

plt.xlabel(“year-month”)
plt.ylabel(“Precipitation (mm)”)
plt.title(“Max monthly precipition”)
plt.savefig(OutputFolder+“MaxMonthlyPrecip.png”)
plt.show()

···

On Thu, Apr 19, 2012 at 2:52 AM, Goyo <goyodiaz@…287…> wrote:

El día 18 de abril de 2012 07:59, questions anon > > <questions.anon@…287…> escribió:

I am not exactly sure how to use datetime objects instead of strings.

This is the code I am working with at the moment and the code works except

for the dates, they are just weird numbers along the x-axis.

Seems like you’re plotting yearmonthlist in the x axis, which is a

list of strings and each string is the concatenation of the string

representations of two numbers. So numbers in the x axis are to be

expected.

You can create datetime objects this way:

d = datetime.datetime(year, month, 1)

Then create an array of datetime objects and use it as the x parameter to plot.

Goyo

If you can write a standalone, minimal executable script which
reproduces the problem I'll take a look. Send it as an attachement and
add sample data files if necessary.

Goyo

···

El día 19 de abril de 2012 05:31, questions anon <questions.anon@...287...> escribió:

Thank you, I was able to get it to work but only if I imported datetime
within the loop, otherwise I ended up with the
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
and if I added 'import datetime' at the top of my script it had an error
where I loop through combining each month
" stop_month = datetime(2011, 03, 01)
TypeError: 'module' object is not callable"

The issue is that there is a slight mixup in namespaces. There is a module called datetime, and that module contains a class object called datetime. So, if your imports at the top are “import datetime”, then all your module-related stuff need to be prepended with “datetime.”. But, if your imports at the top are “from datetime import datetime”, then you can use the object freely, but you can’t use anything else from the module unless you also import it.

Here is the tricky part. In your code, you did the following:

from datetime import datetime

If you then did:

import datetime

depending on the order the two were, one would overwrite the other. You can only have one thing called “datetime”. Personally, I would do one of two things:

import datetime as dt

and use “dt.datetime()” to create datetime objects as well as call functions like “dt.strftime()”. Or, do

from datetime import datetime, date, timedelta, strftime

and get replace calls like “datetime.datetime()” and “datetime.strftime()” with just “datetime()” and “strftime()”.

I hope that clears things up. Namespaces are a honking good idea, but having objects be the same exact name as a module gets confusing very easily.

Cheers!
Ben Root

···

On Thu, Apr 19, 2012 at 1:21 PM, Goyo <goyodiaz@…1896…> wrote:

El día 19 de abril de 2012 05:31, questions anon > > <questions.anon@…287…> escribió:

Thank you, I was able to get it to work but only if I imported datetime

within the loop, otherwise I ended up with the

AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’

and if I added ‘import datetime’ at the top of my script it had an error

where I loop through combining each month

" stop_month = datetime(2011, 03, 01)

TypeError: ‘module’ object is not callable"

If you can write a standalone, minimal executable script which

reproduces the problem I’ll take a look. Send it as an attachement and

add sample data files if necessary.

Goyo

thanks for all the responses. still finding it very confusing!! but got it
to work (without having to import in the loop).
I used:
from datetime import datetime as dt

but I also had to call:
from datetime import timedelta

and now it seems to work nicely. thank you

···

On Fri, Apr 20, 2012 at 3:36 AM, Benjamin Root <ben.root@...1304...> wrote:

On Thu, Apr 19, 2012 at 1:21 PM, Goyo <goyodiaz@...287...> wrote:

El día 19 de abril de 2012 05:31, questions anon >> <questions.anon@...287...> escribió:
> Thank you, I was able to get it to work but only if I imported datetime
> within the loop, otherwise I ended up with the
> AttributeError: type object 'datetime.datetime' has no attribute
'datetime'
> and if I added 'import datetime' at the top of my script it had an error
> where I loop through combining each month
> " stop_month = datetime(2011, 03, 01)
> TypeError: 'module' object is not callable"

If you can write a standalone, minimal executable script which
reproduces the problem I'll take a look. Send it as an attachement and
add sample data files if necessary.

Goyo

The issue is that there is a slight mixup in namespaces. There is a
module called datetime, and that module contains a class object called
datetime. So, if your imports at the top are "import datetime", then all
your module-related stuff need to be prepended with "datetime.". But, if
your imports at the top are "from datetime import datetime", then you can
use the object freely, but you can't use anything else from the module
unless you also import it.

Here is the tricky part. In your code, you did the following:

from datetime import datetime

If you then did:

import datetime

depending on the order the two were, one would overwrite the other. You
can only have one thing called "datetime". Personally, I would do one of
two things:

import datetime as dt

and use "dt.datetime()" to create datetime objects as well as call
functions like "dt.strftime()". Or, do

from datetime import datetime, date, timedelta, strftime

and get replace calls like "datetime.datetime()" and "datetime.strftime()"
with just "datetime()" and "strftime()".

I hope that clears things up. Namespaces are a honking good idea, but
having objects be the same exact name as a module gets confusing very
easily.

Cheers!
Ben Root