2016-02-14 2 views
5

私は、既定のフォーマットとして現れる図をプロットしています:月はダニように私はそれを修正したいと思いmatplotlibの日付操作年ティックは12ヶ月ごとに表示するよう

Year tick shows up every 12 months, but months show only every 3 months

を1か月ごとに表示されますが、年を保ちます。私の現在の試みはこれです:

years = mdates.YearLocator() 
months = mdates.MonthLocator() 
monthsFmt = mdates.DateFormatter('%b-%y') 
dts = s.index.to_pydatetime() 

fig = plt.figure(); ax = fig.add_subplot(111) 
ax.plot(dts, s) 
ax.xaxis.set_major_locator(months) 
ax.xaxis.set_major_formatter(monthsFmt) 

が、正しい結果生成していないです:それは最初のようですが、数ヶ月で現れるように、私はそれを修正する必要がありますどのように正確に

Not right

をダニは毎月表示されますか?

答えて

7

マイナーダニに数ヶ月を当てて、メジャーとして年をとどめる1つの解決策を考え出します。

など。

years = mdates.YearLocator() 
months = mdates.MonthLocator() 
monthsFmt = mdates.DateFormatter('%b') 
yearsFmt = mdates.DateFormatter('\n\n%Y') # add some space for the year label 
dts = s.index.to_pydatetime() 

fig = plt.figure(); ax = fig.add_subplot(111) 
ax.plot(dts, s) 
ax.xaxis.set_minor_locator(months) 
ax.xaxis.set_minor_formatter(monthsFmt) 
plt.setp(ax.xaxis.get_minorticklabels(), rotation=90) 
ax.xaxis.set_major_locator(years) 
ax.xaxis.set_major_formatter(yearsFmt) 

結果で: enter image description here

関連する問題