简体   繁体   English

如何在热图的每个块之间添加间距

[英]How to add spacing between each block of a heatmap

I have a matrix like this.我有一个这样的矩阵。 Is there a way I can add some white lines between it without changing the scaling.有没有一种方法可以在不改变缩放比例的情况下在它之间添加一些白线。 I have used matplotlib for this graph:我为这张图使用了 matplotlib:
我有一个这样的矩阵。有没有一种方法可以在不改变缩放比例的情况下在它之间添加一些白线。我为这张图使用了 matplotlib

Something like this should work.:这样的事情应该有效。:
这样的事情应该有效。

Code ->代码 ->

# final heatmap code with correct color range
import matplotlib.pyplot as plt
import numpy as np


def heatmap2d(arr: np.ndarray):
    plt.imshow(arr, cmap='jet', interpolation = "none",vmin = 140, vmax = 395)
    plt.colorbar()
    plt.show()


test_array = [
     [220, 152, 146, 151, 146, 144],
     [142, 156, 290, 174, 152, 151],
     [148, 190, 390, 370, 146, 152],
     [143, 142, 380, 375, 146, 152],
     [154, 146, 154, 172, 150, 152],
     [150, 152, 144, 140, 142, 0]
 ]
heatmap2d(test_array)

Seaborn makes this pretty easy. Seaborn 使这变得非常容易。 Call sns.heatmap , and pass the linewidths parameter.调用sns.heatmap ,并传递linewidths参数。

import seaborn as sns
import numpy as np


def heatmap2d(arr: np.ndarray):
    sns.heatmap(test_array, linewidths=20, square=True, vmin=140, vmax=395, cmap='jet')


test_array = [
     [220, 152, 146, 151, 146, 144],
     [142, 156, 290, 174, 152, 151],
     [148, 190, 390, 370, 146, 152],
     [143, 142, 380, 375, 146, 152],
     [154, 146, 154, 172, 150, 152],
     [150, 152, 144, 140, 142, 0]
 ]
heatmap2d(test_array)

在此处输入图像描述

Following the tutorial of matplotlib , you can add extra minor ticks on the axes and apply a white grid on those minor ticks.按照matplotlib 的教程,您可以在轴上添加额外的小刻度,并在这些小刻度上应用白色网格。 It causes a padding effect on your heatmap like plot它会对您的热图产生填充效果,例如 plot

在此处输入图像描述

# Turn spines off and create white grid.
ax.spines[:].set_visible(False)
ax.set_xticks(np.arange(data.shape[1]+1)-.5, minor=True)
ax.set_yticks(np.arange(data.shape[0]+1)-.5, minor=True)
ax.grid(which="minor", color="w", linestyle='-', linewidth=3) # change the appearance of your padding here
ax.tick_params(which="minor", bottom=False, left=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM