2017-10-20 90 views
0

我有日期索引溫度的數據幀值的Python matplotlib不顯示鼠標懸停全日

Date  Temperature 
2015-10-21  9.118 
2015-10-22  9.099 
2015-10-23  8.945 
2015-10-26  8.848 
2015-10-27  8.848 
2015-10-28  8.829 
2015-10-30  8.935 
2015-11-02  9.302 
2015-11-03  9.012 
2015-11-04  9.109 
... 
2017-10-16 10.160 
2017-10-17 10.180 
2017-10-18 10.140 
2017-10-19 10.370 
2017-10-20 10.360 

使用交互式%matplotlib筆記本繪製它顯示的溫度,年/月在右下角,但省略了當天

#%matplotlib notebook 
import pandas as pd 
import matplotlib.pyplot as plt 

x_axis = df.index.tolist() 
y_axis = df['temperature'].tolist() 

plt.plot(x_axis, y_axis) 
plt.show() 

enter image description here

我怎樣才能得到它顯示的一天嗎?任何格式都可以,但最好是'01-Jan-2017'。

回答

0

您可以使用軸的format_coord方法來指定狀態欄或筆記本電腦圖左下角顯示的文本格式。

如果這是一個日期時間對象,則使用matplotlib.dates.DateFormatter是合理的。格式說明符然後是"%d-%b-%Y",例如'01-Jan-2017'。

import matplotlib.dates 
datefmt = matplotlib.dates.DateFormatter("%d-%b-%Y") 
fmt = lambda x,y : "{}, {:.5g}".format(datefmt(x), y) 
plt.gca().format_coord = fmt 

enter image description here