代码之家  ›  专栏  ›  技术社区  ›  Bhuvan Kumar

使用matplotlib和Pandas数据帧绘制数据覆盖图

  •  2
  • Bhuvan Kumar  · 技术社区  · 7 年前

    我已经在pandas数据框中创建了时间序列的数据覆盖率,并希望在Matplotlib或PyQtGraph中绘制数据覆盖率。

    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)
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   MaxU - stand with Ukraine    7 年前

    您可以使用 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

    更新:

    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