How to add spacing between each block of a heatmap

Viewed 87

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)
1 Answers

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

Related