提高热图的“精度”

问题描述 投票:0回答:1

使用

seaborn
我正在为 DataFrame 的各个系列生成热图。

我使用的主要代码如下:

def plot_heatmap(data, cmap, x_label, y_label, y_ticks = None):

    fig, ax = plt.subplots()

    heatmap = sns.heatmap([data], robust=True,cmap=cmap, annot=False, cbar_kws={"label": "Value"}, ax=ax)

    ax.set_xlabel(x_label)
    ax.set_ylabel(y_label)
    if y_ticks:
        heatmap.set_yticklabels([y_ticks])

    plt.show()

它是根据我所拥有的情况(对于每个 Pandas.Series)调用的。我的问题是生成的结果如下所示:

但事实是,这看起来有些欺骗性。我为此图生成了以下随机数据集:数据集要点

你看到的零的数量是 1。但是观察这个图的人可能会有不同的想法。

那么,我该如何改进这个图以使其更加准确呢?我尝试了各种 cmap,例如:

    cmap = sns.diverging_palette(220, 10, as_cmap=True) 
    cmap2 = sns.diverging_palette(250, 0, sep=1, n=256, as_cmap=True)
    cmap3 = 'gist_rainbow'
    ...

但我并没有完全得到想要的结果。预先感谢您!

matplotlib plot seaborn heatmap
1个回答
0
投票

你的尺度范围很广。您可以尝试绘制数据的对数:

plot_heatmap(np.log10(df.data), cmap="plasma", x_label="asdf", y_label="asdf")

和/或从数据创建类别:

bins = np.logspace(np.log10(0.1), np.log10(df["data"].max()), 20)
labels = np.arange(1, len(bins))

df['binned'] = pd.cut(df['data'], bins=bins, labels=labels)

plot_heatmap(df["binned"], cmap="magma", x_label="asdf", y_label="asdf")

© www.soinside.com 2019 - 2024. All rights reserved.