6 Tips To Customize Seaborn Correlation Heatmaps
One of the important steps of exploratory data analysis includes analyzing the correlation matrix. Heatmaps are generally used for visualization of correlation matrices.
A correlation heatmap shows 2D correlation matrix between two discrete dimensions with the help of colored cells which usually represent data from a monochromatic scale. The color of each cell is proportional to the number of measurements that match the respective dimensional value. This gives a way to overview all the numeric values with an visual approach which is not only easily comprehensible but also very visually appealing.
A correlation heatmap can be easily plotted using Seaborn which is a Python data visualization library that is based on matplotlib. Here are 6 tips for basic customization of seaborn correlation heatmaps that can help you make your visualizations look better.
Let’s start by importing necessary libraries.
Here’s how the full Seaborn heatmap function looks like:
seaborn.heatmap(data, *, vmin=None, vmax=None, cmap=None, center=None, robust=False, annot=None, fmt=’.2g’, annot_kws=None, linewidths=0, linecolor=’white’, cbar=True, cbar_kws=None, cbar_ax=None, square=False, xticklabels=’auto’, yticklabels=’auto’, mask=None, ax=None, **kwargs)
Plotting a basic Seaborn heatmap:
1. Customizing the color bar
a. Customizing label and orientation
Use cbar_kws={‘label’: ‘my_color_bar’, 'orientation': 'horizontal'}
to customize color bar label and make its orientation horizontal (orientation is vertical by default).
b. Removing color bar
Remove the color bar by setting cbar=False
.
2. Annotations over heatmap and its font size
Add annotations over heatmap to represent data value of each cell by setting annot=True
. Set the font size using sns.set(font_scale=0.8)
. Use fmt=’.1g’
to display most of the annotations up to only 1 decimal places in order to improve readability.
3. Customizing x-axis and y-axis labels
Customize x-axis label by adding the line plt.xlabel(“my_x_axis_label”)
and for y-axis add plt.ylabel(“my_y_axis_label”)
.
4. Removing x-axis and y-axis tick labels
Set xticklabels=False
to remove x-axis tick labels and yticklabels=False
to remove y-axis tick labels.
5. Changing color of the heatmap
a. By using Sequential color maps
Other sequential color maps that can be used:
b. By using Seaborn color palettes
Some seaborn color palettes:
c. By using Seaborn Diverging palettes
Some other diverging palettes:
6. Customizing heatmap border
Use the linewidth
and linecolor
parameters.
You can find code to all the visualizations in my Kaggle notebook. Hope this helped. Have a nice day!