Create heatmap using pandas TimeSeries

后端 未结 1 1219
半阙折子戏
半阙折子戏 2020-12-05 15:55

I need to create MatplotLib heatmap (pcolormesh) using Pandas DataFrame TimeSeries column (df_all.ts) as my X-axis.

How to convert Pandas TimeSeries column to someth

相关标签:
1条回答
  • 2020-12-05 16:40

    I do not know what you mean by heat map for a time series, but for a dataframe you may do as below:

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    
    from itertools import product
    from string import ascii_uppercase
    from matplotlib import patheffects
    
    m, n = 4, 7 # 4 rows, 7 columns
    df = pd.DataFrame(np.random.randn(m, n),
                      columns=list(ascii_uppercase[:n]),
                      index=list(ascii_uppercase[-m:]))
    
    
    ax = plt.imshow(df, interpolation='nearest', cmap='Oranges').axes
    
    _ = ax.set_xticks(np.linspace(0, n-1, n))
    _ = ax.set_xticklabels(df.columns)
    _ = ax.set_yticks(np.linspace(0, m-1, m))
    _ = ax.set_yticklabels(df.index)
    
    ax.grid('off')
    ax.xaxis.tick_top()
    

    optionally, to print actual values in the middle of each square, with some shadows for readability, you may do:

    path_effects = [patheffects.withSimplePatchShadow(shadow_rgbFace=(1,1,1))]
    
    for i, j in product(range(m), range(n)):
        _ = ax.text(j, i, '{0:.2f}'.format(df.iloc[i, j]),
                    size='medium', ha='center', va='center',
                    path_effects=path_effects)
    

    heat-map

    0 讨论(0)
提交回复
热议问题