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

发布于 2025-02-11 02:02:16 字数 884 浏览 3 评论 0原文

我有这样的矩阵。有什么方法可以在不更改缩放的情况下之间添加一些白线。我已经使用matplotlib作为此图:

代码 - >

# 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)

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:
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

Something like this should work.:
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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追我者格杀勿论 2025-02-18 02:02:16

Seaborn使这很容易。调用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)

Seaborn makes this pretty easy. Call sns.heatmap, and pass the linewidths parameter.

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)

enter image description here

獨角戲 2025-02-18 02:02:16

遵循,您可以在轴上添加额外的小滴答,并在这些小滴答上涂上白色网格。它会对您的热图产生填充效果,例如图

”在此处输入图像描述

# 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)

Following the tutorial of matplotlib, you can add extra minor ticks on the axes and apply a white grid on those minor ticks. It causes a padding effect on your heatmap like plot

enter image description here

# 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文