2012-02-29 84 views
3

我已經繪製了在特定地點超過16年的碳通量時間序列。我希望X軸有年(1992-2007)而不是年份(1-16)。當我將x軸設置爲1992的最小值和2007的最大值時,該圖不會出現在圖上,但是當我不設置最小/最大年時,它就會出現。我不知道我做錯了什麼。我繪製了一年以上的另一個時間序列,並且能夠使用MonthLocator爲月份標記x軸,但是沒有YearLocator的運氣。這裏是我寫的代碼:使用matplotlib設置x軸的最小/最大年份

fig=pyplot.figure() 
ax=fig.gca() 
ax.plot_date(days,nee,'r-',label='model daily nee') 
ax.plot_date(days,nee_obs,'b-',label='obs daily nee') 

# locate the ticks 
ax.xaxis.set_major_locator(YearLocator()) 

# format the ticks 
ax.xaxis.set_major_formatter(DateFormatter('%Y')) 

# set years 1992-2007 
datemin = datetime.date(1992, 1, 1) 
datemax = datetime.date(2007, 12, 31) 
ax.set_xlim(datemin, datemax) 

labels=ax.get_xticklabels() 
setp(labels,'rotation',45,fontsize=10) 

legend(loc="upper right", bbox_to_anchor=[0.98, 0.98], 
     ncol=1, shadow=True) 

pyplot.ylabel('NEE($gC m^{-2} day^{-1}$)') 
pyplot.title('Net Ecosystem Exchange') 

pyplot.savefig('nee_obs_model_HF_daily.pdf') 

# rotates and right aligns the x labels, and moves the bottom of the 
# axes up to make room for them 
#fig.autofmt_xdate() 

pyplot.show() 
pyplot.close() 
+2

你的天'數組是如何構建的? – 2012-02-29 15:47:03

+0

你能否提供'days','nee'和'nee_obs'的一些樣本數據。請同時發佈您的導入信息,以便我不必查找'YearLocator'和'DateFormatter'來自'matplotlib.dates',並且我可以在'pyplot'中找到'setp'和'legend'。 – BioGeek 2012-02-29 15:53:15

回答

1

我認爲安德烈索博列夫是正確的。當我運行你的腳本時,只需稍作調整:-),用日期字段作爲日期的一些數據,我可以看到幾年沒有問題出現。這無形中你的代碼,以除外:

fh = open(thisFileName) 
# a numpy record array with fields: date, nee, nee_obs 
# from a csv, thisFileName with format: 
# Date,nee,nee_obs 
# 2012-02-28,137.20,137.72 
matplotlib.mlab.csv2rec(fh) 
fh.close() 
r.sort() 
days = r.date 
nee = r.nee 
nee_obs = r.nee_obs 
... 
... 

,然後我得到: NEE Figure

許多這樣的解決方案,從​​什麼借來的。如果我誤解了你的需要,請告訴我。

+0

謝謝你的回覆。這是我的日子列表中的問題。我最初創建它的天數=範圍(1,5844),然後我厭倦了使用YearLocator來格式化x軸。相反,我寫了一個函數來創建一個從1992年到2007年的天數列表。 – Darren 2012-03-01 12:56:26

+0

DEF create_years(): \t日期= [] \t \t #天16年天=範圍(0,5843)\t \t \t \t \t 對於i在天: \t \t datestart =日期時間。日期(1992年,1,1) \t \t dates.append(datestart + datetime.timedelta(i))的 \t \t #PRINT datestart + datetime.timedelta(ⅰ) \t返回日期 – Darren 2012-03-01 13:05:50

相關問題