Seaborn Heatmap without the white space surrounding the plot

Viewed 1593

I'm trying to create a heatmap with seaborn. I want to save the heatmap plot as an image without the colorbar and without the white space surrounding the plot to use it for further analysis.

my_np = np.array([[1,2,4,2,3,5,6,10], [1,3,8,2,9,5,6,9],[1,4,4,6,7,5,8,1],[2,0,3,5,7,8,9,1], [2,9,4,3,7,5,8,8], [3,9,4,3,7,5,8,8],[3,9,4,3,7,5,8,8],[3,1,4,4,0,5,8,8],[3,9,0,3,7,5,8,8]])

sns.heatmap(my_np)

I tried the bbox_inches='tight', pad_inches=0 but it didn't work

Bellow an example on my real array enter image description here

I'm trying to have enter image description here

1 Answers

If you set bbox_inches='tight' and pad_inches=0.01 as saving parameters, images without white edges will be saved.

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
sns.set()

my_np = np.array([[1,2,4,2,3,5,6,10], [1,3,8,2,9,5,6,9],[1,4,4,6,7,5,8,1],[2,0,3,5,7,8,9,1], [2,9,4,3,7,5,8,8], [3,9,4,3,7,5,8,8],[3,9,4,3,7,5,8,8],[3,1,4,4,0,5,8,8],[3,9,0,3,7,5,8,8]])

ax = sns.heatmap(my_np, cbar=False)
ax.axis('off')

plt.savefig('./nowhitespace.png', bbox_inches='tight', pad_inches=0.01)
plt.show()

enter image description here

Related