Curriculum
Course: Data Science
Login

Curriculum

Data Science

Text lesson

Using a Heatmap

A heatmap can be used to visualize the correlation between variables.

img_stat_heatmap

The closer the correlation coefficient is to 1, the greener the squares appear. The closer it is to -1, the browner the squares become.

Use Seaborn to Create a Heatmap

We can use the Seaborn library to generate a correlation heatmap, as it is a visualization library built on top of matplotlib.

Example

import matplotlib.pyplot as plt
import seaborn as sns

correlation_full_health = full_health_data.corr()

axis_corr = sns.heatmap(
correlation_full_health,
vmin=-1, vmax=1, center=0,
cmap=sns.diverging_palette(50500, n=500),
square=True
)

plt.show()

Example Explained:

  • Import the seaborn library as sns.
  • Use the full_health_data set.
  • Call sns.heatmap() to generate a heatmap visualizing the correlation matrix.
  • Define the range of values for the heatmap, with 0 as the center.
  • Set the color palette using sns.diverging_palette, where n=500 specifies 500 color variations.
  • Set square=True to display the heatmap in square-shaped cells.