2016-11-15 365 views
1

我試圖繪製以pandas timedelta值爲索引的數據框中的某些列。當我繪製它時,即使在兩者之間存在可變時間,所有點也沿x軸均勻分佈。使用pandas繪製時間軸時的間隔間距timedelta

time = [pd.Timestamp('9/3/2016')-pd.Timestamp('9/1/2016'),pd.Timestamp('9/8/2016')-pd.Timestamp('9/1/2016'),pd.Timestam\p('9/29/2016')-pd.Timestamp('9/1/2016')] 
df = pd.DataFrame(index=time, columns=['y'],data=[5,0,10]) 
df.plot() 
plt.show() 

Wrong spacing

相反,如果我使用的日期,而不是timedelta,我得到了適當的間距x軸:

time = [pd.Timestamp('9/3/2016'),pd.Timestamp('9/5/2016'),pd.Timestamp('9/20/2016')] 
df = pd.DataFrame(index=time, columns=['y'],data=[5,0,10]) 
df.plot() 
plt.show() 

Right spacing

有沒有辦法讓這個正確顯示?

回答

0

目前,它還沒有完全支持熊貓。有關更多信息,請參閱this issue on Github

對於一個快速的解決方法,您可以使用:

import matplotlib.pyplot as plt 
plt.plot(df.index, df.values) 

這裏是你如何能與蜱發揮讓他們讀的例子(而不是隻是一個非常大的數字)

import matplotlib as mpl 
import datetime 
fig, ax = plt.subplots() 
ax.plot(df.index, df.values) 
plt.xticks([t.value for t in df.index], df.index, rotation=45) 
plt.show() 

result

+0

非常感謝! –

+0

剛剛添加了一個如何格式化蜱的例子......因爲他們默認看起來很垃圾(基本上是一個非常大的數字......)。 如果這很有幫助,請回答問題並接受,如果它解決了您的問題。謝謝! –