2017-09-25 101 views
1

我想在x = '23:30:00'處顯示垂直線。我也嘗試過x = '23:30'。有任何想法嗎?如何向matplotlib添加垂直線?

df1 = df1.between_time('19:30','23:59') 
df1['high'].plot(kind='line',figsize = (10,5)) 
plt.axvline(x='23:30:00', color = 'r') 
plt.show() 

Add vert line to chart at 23:30

+1

您是否嘗試過使用實際的日期時間實例作爲x變量? – Evert

回答

5

你應該通過你想在日期時間格式的行,而不是傳遞字符串的位置。簡單地說:

#datetime format input 
plt.axvline(x= datetime.datetime(2017, 9, 25,23,30), color = 'r') 

結果在隨機數據幀:

enter image description here

+0

我必須缺少一些東西,我得到一個錯誤「名稱日期時間沒有定義」,我想只輸入時間,這就是垂直線的位置? –

+1

您需要先導入日期時間庫。在使用之前寫入[import datatime]。 –

+0

@Ross Demtschyna如果您解決了您的問題,請接受並關閉此問題 –