4.4.3 Heatmap
The heatmap is a way of representing the data in a 2-dimensional form. The data values are represented as colors in the graph. The goal of the heatmap is to provide a colored visual summary of information.
1. Heatmap and Annotation
We will create a random dataset and a subplot. The left one is the default mode heatmap, the right one shows the annotation.
# Create a random dataset
data = np.random.rand(5, 10)
f,axes = plt.subplots(1,2, figsize = (20,6))
sns.heatmap(data, ax=axes[0])
sns.heatmap(data, annot=True, ax=axes[1])

2. Horizontal Bar Heatmap
Meanwhile, we can change the color bar to horizontal.
sns.heatmap(data, annot=True,
cbar_kws={'label': 'Horizontal Colorbar', 'orientation': 'horizontal'})

f,axes = plt.subplots(1,2, figsize = (20,6))
sns.heatmap(data,cmap="YlGnBu",ax=axes[0])
sns.heatmap(data,cmap="YlGnBu",annot=True, cbar=False,ax=axes[1])

3. Categorical Heatmap (in Triangle)
# create a simple dataset,giving the column names.
d = pd.DataFrame(data,
columns=['A','B','C','D','E','F','G','H','I','J'])
# Compute the correlation matrix
corr = d.corr()
# Generate a mask for the upper triangle
mask = np.triu(np.ones_like(corr, dtype=np.bool))
# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 10, as_cmap=True)
# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr,mask = mask,cmap=cmap, vmax=.3, center=0,
square=True, linewidths=.5, cbar_kws={"shrink": .5})

Last updated
Was this helpful?