2012-02-29 21 views
3

私は特定の場所で16年以上の炭素フラックスの時系列をプロットしました。年番号(1〜16)の代わりにx軸に年(1992〜2007年)が必要です。私がx軸を1992の最小値と2007の最大値に設定すると、グラフはプロットに表示されませんが、最小/最大の年を設定しないと表示されます。私は何が間違っているのか分かりません。私は1年以上の別のtimeseriesをプロットし、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

'days'配列はどのように構築されていますか? –

+0

'days'、' nee'、 'nee_obs'のサンプルデータを提供できますか? 'matplotlib.dates'の' YearLocator'と 'DateFormatter'が' pyplot'に 'setp'と' legend'を見つけることができるようにインポートを投稿してください。 – BioGeek

答えて

1

私はAndrey Sobolevが正しいと思います。あなたのスクリプトを微調整して、:-)、私は日付として日付フィールドを持っているいくつかのデータで、私は何の問題もなく現れます。それはを除いて、事実上、あなたのコードです:

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

返信いただきありがとうございます。私の日のリストには問題がありました。もともと私はdays = range(1,5844)で作成していましたが、次にYearLocatorを使用してx軸をフォーマットするのに疲れました。代わりに、私は1992年から2007年の日のリストを作成する関数を書いた。 – Darren

+0

DEF create_years():16年 \t日付= [] \t \t#日 \t日=範囲(0,5843)iに対する\t \t \t \t \t日: \t \t datestart =日時。日付(1992、1、1) \t \t dates.append(datestart + datetime.timedelta(i))を \t \t #printのdatestart + datetime.timedelta(I) \t返品日 – Darren

関連する問題