Clustered heatmaps
Heatmaps are extremely useful to visualize a correlation matrix, but clustermaps are better. A Clustermap allows to uncover structure in a correlation matrix by producing a hierarchically-clustered heatmap:
df_corr = df.corr()
fig = sns.clustermap(df_corr)
plt.setp(fig.ax_heatmap.xaxis.get_majorticklabels(), rotation=90)
plt.setp(fig.ax_heatmap.yaxis.get_majorticklabels(), rotation=0)
To prevent overlapping of axis labels, you can reference the Axes
from the underlying fig
object and specify the rotation. You can learn about the arguments to the clustermap()
function here.
This exercise is part of the course
Visualizing Time Series Data in Python
Exercise instructions
- Import
seaborn
assns
. - Compute the correlation between all columns in the
meat
DataFrame using the Pearson method and assign the results to a new variable calledcorr_meat
. - Plot the clustermap of
corr_meat
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Import seaborn library
____
# Get correlation matrix of the meat DataFrame
corr_meat = ____(____)
# Customize the heatmap of the corr_meat correlation matrix and rotate the x-axis labels
fig = ____(corr_meat,
row_cluster=True,
col_cluster=True,
figsize=(10, 10))
plt.setp(fig.ax_heatmap.xaxis.get_majorticklabels(), rotation=90)
plt.setp(fig.ax_heatmap.yaxis.get_majorticklabels(), rotation=0)
plt.show()