2017-03-15 16 views
0

私はx軸がmatplotlibを使用して日付である多軸折れ線グラフを作成しようとしています。下の図に見られるように、線は近いですが、すべてが正しくない左の軸で終わっているようです。ここでmatplotlibを使用した複数軸timeseries折れ線グラフ

は私のコードです:

df.Date = pd.to_datetime(df.Date) 
fig, ax = plt.subplots() 
ax2= ax.twinx() 
ax2.set_frame_on(True) 
ax2.patch.set_visible(False) 
fig.subplots_adjust(right=0.75) 

years = YearLocator() # every year 
months = MonthLocator() # every month 
yearsFmt = DateFormatter('%Y') 

ax.plot_date(df.Date,df.A, fmt="r-") 
ax.plot_date(df.Date,df.B, fmt="b-") 
ax2.plot_date(df.Date,df.C, fmt="y-") 
ax2.plot_date(df.Date,df.D, fmt="g-") 
ax.xaxis.set_major_locator(years) 
ax.xaxis.set_major_formatter(yearsFmt) 
ax.xaxis.set_minor_locator(months) 
ax.autoscale_view() 
ax2.xaxis.set_major_locator(years) 
ax2.xaxis.set_major_formatter(yearsFmt) 
ax2.xaxis.set_minor_locator(months) 
ax2.autoscale_view() 
plt.setp(ax.get_xticklabels(), fontsize=10, rotation='vertical') 
plt.setp(ax2.get_xticklabels(), fontsize=10, rotation='vertical') 
ax.fmt_xdata = DateFormatter('%b\n%Y') 
ax2.fmt_xdata = DateFormatter('%b\n%Y') 
fig.autofmt_xdate() 

plt.setp(ax.get_xticklabels(), fontsize=10, rotation='vertical') 
ax.set_ylabel('(%)') 
ax2.set_ylabel('(%)') 
ax2.set_xlabel('Date') 
plt.title('Chart 1. ', fontsize=8, weight= 'bold') 
plt.tight_layout() 
plt.show() 

Chart 1

答えて

0

(= '日' で)DF1 = df.sort_valuesを使用する必要があります。私がデータを詳しく見てみると、データセットの終わりに向かって順番に並んでいないいくつかの日付があったため、プロットは2002年に戻ってグラフの左側に移動しました。

関連する問題