From: questions a. <que...@gm...> - 2012-04-18 05:59:57
|
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 <goy...@gm...> wrote: > El día 12 de abril de 2012 03:46, questions anon > <que...@gm...> 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 > |