From: questions a. <que...@gm...> - 2012-04-19 03:32:01
|
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 <goy...@gm...> wrote: > El día 18 de abril de 2012 07:59, questions anon > <que...@gm...> 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 > |