2016-11-17 6 views
0

"01/03/1991"形式の日付を表示するグラフプロットを生成したいが、私のコードで日付を取得している「Jan 03 1991」と同じです。 以下はグラフを生成するために使用しているコードです。誰も私がこの問題を解決するのを助けることができます。x軸の日付をm/d/Y形式でプロットする

import numpy as np 
import matplotlib.pyplot as plt 
import datetime as dt 
import matplotlib.dates as mdates 

dates = ['01/02/1991','01/03/1991','01/04/1991', '01/05/1991','01/06/1991','01/07/1991'] 

a = [0, 2.0, 3.0, 4.0, 5.0, 10.0] 
r = [dt.datetime.strptime(d,'%m/%d/%Y').date() for d in dates] 
s = [1.0, 4.0, 9.0, 16.0, 25.0, 40.0] 

fig, ax1 = plt.subplots() 

ax2 = ax1.twinx() 
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y')) 
plt.gca().xaxis.set_major_locator(mdates.DayLocator()) 
lns1 = ax1.plot(r, a, marker='o', label='No. of trip', linewidth=4) 
lns2 = ax2.plot(r, s, marker='o', linestyle='--', color='r', label='No. of customer', linewidth=4) 
plt.gcf().autofmt_xdate() 

ax1.set_xlabel('x') 
ax1.set_ylabel('y1', color='b') 
ax2.set_ylabel('y2', color='r') 

# added these three lines 
lns = lns1+lns2 
labs = [l.get_label() for l in lns] 
ax2.legend(lns, labs, loc=0) 
plt.show() 

答えて

1

不適切なオブジェクトにフォーマッタを適用しました。あなたはそう

ax1.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y')) 
+0

はい、私は以前にそのように行ったよう

ax1 

に適用されなければならないが、それは私にエラーを与えました。今私はそれを修正しました。どうもありがとうございました – nas