how to plot heat map with python seaborn package

created at 07-01-2021 views: 4

data

A common application scenario of the heat map is to draw the correlation coefficient heat map, and the data prepares a correlation coefficient matrix.

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['figure.dpi'] = 80 # Graphic resolution
pd.options.display.notebook_repr_html=False # Table display

Correlation coefficient matrix

np.random.seed(1) # Random seed
mat=pd.DataFrame(np.random.rand(3,6),columns=list('abcdef')).corr()
mat
          a         b         c         d         e         f
a  1.000000  0.297407 -0.610065 -0.908297 -0.997893 -0.991160
b  0.297407  1.000000 -0.937936  0.129262 -0.234835 -0.421449
c -0.610065 -0.937936  1.000000  0.222660  0.557370  0.709796
d -0.908297  0.129262  0.222660  1.000000  0.933525  0.844767
e -0.997893 -0.234835  0.557370  0.933525  1.000000  0.980463
f -0.991160 -0.421449  0.709796  0.844767  0.980463  1.000000

plot

Call the heatmap method to draw a heat map.

sns.heatmap(mat)
plt.show()

plot

Adjust the color palette

Set the vmin and vmax parameters to adjust the lower limit and upper limit of the palette.

# Set the upper and lower limits of the palette
sns.heatmap(mat,vmin=0,vmax=1)
plt.show()

Adjust the color palette

Set cmap parameters, you can modify the palette style.

sns.heatmap(mat,cmap='YlGnBu')
plt.show()

Set cmap parameters

Hide legend

Set the parameter cbar=False to hide the legend.

sns.heatmap(mat,cbar=False)
plt.show()

Hide legend

Display value

Set the parameter annot=True to display the specific value on the heat map, and set the fmt parameter to modify the style of the value display.

sns.heatmap(mat,annot=True,fmt='.3f')
plt.show()

Display value

Please log in to leave a comment.