2017-08-29 125 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