2017-08-29 122 views
2

我已經創建了數據覆蓋了pandas數據框中的時間序列,並希望繪製Matplotlib或PyQtGraph中的數據覆蓋率。使用matplotlib和Pandas DataFrame的數據覆蓋圖

數據幀

DateTime WD98 WS120 WS125B WD123 WS125A 
31-07-2013 100 99.9 99.9 NaN NaN 
31-08-2013 100 100 100 NaN NaN 
30-09-2013 100 100 100 NaN NaN 
31-10-2013 100 100 100 NaN NaN 
30-11-2013 100 100 100 100 100 
31-12-2013 100 100 100 100 100 
31-01-2014 100 100 100 100 100 
28-02-2014 100 100 100 100 100 
31-03-2014 100 100 100 100 100 
30-04-2014 100 100 100 100 100 
31-05-2014 67.1 100 100 67.1 7.7 
30-06-2014 NaN NaN 100 0 69.2 
31-07-2014 NaN NaN 100 0 100 
31-08-2014 NaN NaN 100 0 96.2 

我想繪製以下方式(斷條圖)

Data Coverage Done in Excel Conditional Formatting

上述陰謀使用Excel條件格式來完成。請幫幫我。

DataCoverage >= 90 (Green) 
DataCoverage >= 75 and DataCoverage < 90 (Yellow) 
DataCoverage < 75 (red) 

回答

2

您可以使用seaborn.heatmap

import seaborn as sns 

df = df.set_index(df.pop('DateTime').dt.strftime('%d-%m-%Y')) 
g = sns.heatmap(df, cmap=['r','y','g'], annot=True, fmt='.0f') 
g.set_yticklabels(g.get_yticklabels(), rotation=0, fontsize=8) 

結果:

enter image description here

UPDATE:修正版本:

x = df.set_index(df['DateTime'].dt.strftime('%d-%m-%Y')).drop('DateTime', 1)  
z = pd.cut(x.stack(), bins=[-np.inf, 75, 90, np.inf], labels=[1.,2.,3.]).unstack().apply(pd.to_numeric)  
g = sns.heatmap(z, cmap=['r','y','g'], fmt='.0f', cbar=False)  
g.set_yticklabels(g.get_yticklabels(), rotation = 0, fontsize = 8) 

結果:

enter image description here