2017-01-06 28 views
1

私のパンダデータフレームでは、時系列データは絶対時間(YYYY-MM-DD形式の日付HH24:MI:SS.nnnnn)でインデックスされます。日付インデックス付き時系列データを使用してx軸上の経過時間をプロット

2017-01-04 16:25:25.143493 58 
2017-01-04 16:25:26.145494 62 
2017-01-04 16:25:27.147495 57 
2017-01-04 16:25:28.149496 51 
2017-01-04 16:25:29.151497 61 

私のプロットのx軸が、最初のサンプルのタイムスタンプに比べて、ある間隔(例えば、「0 10 20 30 40 50」)の増加する値になるように、 ?期間を使用するか、asfreq()を使用して周波数に変換する必要がありますか?または、私はDateFormatterを使用する必要がありますか?

ドキュメントはちょっと混乱していて、良い例はないようです。ほとんどの時系列の例は、月や年などの粗い間隔を中心に回転しているようです。

+0

可能な複製(http://stackoverflow.com/questions/15240003/matplotlib-intelligent-axis-labels-for-timedelta) – Suever

答えて

3

あなたはその後、timedeltaindexにごdatetimeindexを変換することができプロット。

df.index = df.index - df.index[0] 
df.plot() 
[はtimedeltaためのmatplotlibのインテリジェントな軸ラベル]の
+0

ありがとう!これはとてもシンプルだとは信じられない! – user1612443

+0

プロットする秒数が重要な場合は、 'TimeDeltaIndex' - '(df.index-df.index [0])。seconds'の秒属性を使用します –

1

これを行うための組み込みの方法があるかもしれませんが、何あなたがリストの内包内の日付時刻をsubstractingとtotal_seconds()使用することによって達成することができるとします

# Generate a random timeseries 
a = pd.Series(index=pd.date_range("20000101",freq="s",periods=50),data=np.random.randint(0,10,50)) 

# Create a new index by calculating relative difference between the datetimes. 
# If you use total_seconds, you will convert datetimes into integers (which is what you wanted in your question) 
new_index = [(i-a.index[0]) for i in a.index] 

# Apply new index to the original Series 
a.index = new_index 

# plot the data 
a.plot() 
関連する問題