Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate high-resolution heatmap using seaborn?

I have too many features (~100) for correlation, resulting in low resolution figure. How could I improve resolution?

sns.heatmap(Feature_corr, cbar = True,  square = True, annot=False,annot_kws={'size': 15},  cmap= 'coolwarm')
like image 570
LookIntoEast Avatar asked Feb 04 '23 16:02

LookIntoEast


2 Answers

Call figure from matplotlib.pyplot before heatmap and set the image size with figsize, i.e.:

from matplotlib import pyplot
pyplot.figure(figsize=(15, 15)) # width and height in inches
sns.heatmap(Feature_corr, cbar=1, square=1, annot=0, annot_kws={'size': 15}, cmap= 'coolwarm')
like image 76
Pedro Lobito Avatar answered Feb 07 '23 19:02

Pedro Lobito


Using the figure parameters from Matplotlib makes it possible to modify the width, height, font size of axis labels, and dpi. Adjusting those values may help your image resolution.

Additionally, you can play with the parameter annot_kws={"size": 8} from sns.heatmap() to modify the values font size.

DPI represents the number of pixels per inch in the figure. Higher values of DPI improve the resolution.

PREVIOUS

corr = df2.corr() #your dataframe
sns.heatmap(corr, cmap="Blues", annot=True)

Result image without Matplotlib adjustments

Result image without Matplotlib adjustments

With Matplotlib

import matplotlib.pyplot as plt
import seaborn as sns

corr = df2.corr() #your dataframe

# figsize=(6, 6) control width and height
# dpi = 600, I 
plt.figure(figsize=(6, 6), 
           dpi = 600) 
 
# parameter annot_kws={"size": 8} control corr values font size
sns.heatmap(corr, cmap="Blues", annot=True, annot_kws={"size": 8})

plt.tick_params(axis = 'x', labelsize = 12) # x font label size
plt.tick_params(axis = 'y', labelsize = 12) # y font label size

Result:

Example Image result with Matplotlib:

like image 42
Fredy Rojas Avatar answered Feb 07 '23 17:02

Fredy Rojas