块大小不相等的Python热图

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

我有以下数据集:

results=[array([6.06674849e-18, 2.28597646e-03]), array([0.02039694, 0.01245901, 0.01264321, 0.00963068]), array([2.28719585e-18, 5.14800709e-02, 2.90957713e-02, 0.00000000e+00,
       4.22761202e-19, 3.21765246e-02, 8.86959187e-03, 0.00000000e+00])]

我想从中创建一个热图,其外观类似于下图:enter image description here

是否有可能使用seaborn或matplotlib或任何其他绘图软件包来创建这样的图,如果可以,该如何做?

python matplotlib seaborn heatmap
1个回答
0
投票

一种方法是使行长等于np.repeat。仅当所有行的长度是最长行长度的除数时,此方法才能很好地工作。

数据建议使用LogNorm,尽管这样的范数会因样本输入中的零而分散注意力。

一些代码说明了这个想法:

from matplotlib import pyplot as plt
from matplotlib import colors as mcolors
import numpy as np

results = [np.array([6.06674849e-18, 2.28597646e-03]),
           np.array([0.02039694, 0.01245901, 0.01264321, 0.00963068]),
           np.array([2.28719585e-18, 5.14800709e-02, 2.90957713e-02, 0.00000000e+00,
                     4.22761202e-19, 3.21765246e-02, 8.86959187e-03, 0.00000000e+00])]
longest = max([len(row) for row in results])
equalized = np.array( [np.repeat(row, longest // len(row)) for row in results])
# equalized = np.where(equalized == 0, np.NaN, equalized)
norm = mcolors.LogNorm()
heatmap = plt.imshow(equalized, cmap='nipy_spectral', norm=norm, interpolation='nearest',
                     origin='lower', extent=[0, 6000, 0.5, len(results)+0.5])
plt.colorbar(heatmap)
plt.gca().set_aspect('auto')
plt.yticks(range(1, len(results) + 1))
plt.show()

enter image description here

另一个例子是7级(随机数)。输入生成为:

bands = 7
results = [np.random.uniform(0, 1, 2**i) for i in range(1, bands+1)]

example with 7 levels

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