如何在热图上正确显示多条件数据集?

时间:2019-05-26 08:42:31

标签: python numpy matplotlib

我在numpy数组中有以下格式的数据集。每个“列”都是单独的条件。我想显示一个热图,其中每个“列”将对应于该列中的得分范围:

[[ 226 600 3.33 915. 92.6 98.6 ] [ 217 700 3.34 640. 93.7 98.5 ] [ 213 900 3.35 662. 88.8 96. ] ... [ 108 600 2.31 291. 64. 70.4 ] [ 125 800 3.36 1094. 65.5 84.1 ] [ 109 400 2.44 941. 52.3 68.7 ]]

我编写了一个生成热图的函数:

def HeatMap(data):

    #generate heatmap figure
    figure = plt.figure()
    sub_figure = figure.add_subplot(111)
    heatmap = sub_figure.imshow(data, interpolation='nearest',cmap='jet', aspect=0.05)

    #generate color bar
    cbar = figure.colorbar(ax=sub_figure, mappable=heatmap, orientation='horizontal')
    cbar.set_label('Scores')

    plt.show()

此函数生成的内容:

enter image description here

如上所述,可以看出问题出在我的函数中,因为Scores的范围从0到数据集中的最大值2500。如何修改函数,以便显示热图列中的分数是根据其范围而不是整个数据集的范围?我的第一个想法是将数组尺寸更改为[[226],[600]] etc.之类的东西,但不确定是否能解决问题

感谢您的帮助

1 个答案:

答案 0 :(得分:2)

每列都不能有单独的cmap

如果要查看各列在各自范围内的变化,可以在绘制normalize之前按列heatmap进行数据绘制。

代码

import numpy as np

x = np.array([[1000,  10,   0.5],
              [ 765,   5,  0.35],
              [ 800,   7,  0.09]])

x_normed = x / x.max(axis=0)

print(x_normed)
# [[ 1.     1.     1.   ]
#  [ 0.765  0.5    0.7  ]
#  [ 0.8    0.7    0.18 ]]

# Plot the heatmap for x_normed.

这将保留每列中的变化。