How to Create Correlation Matrix in Pandas Python

Spread the love

In this post, you will learn How to create Correlation Matrix in Pandas.

pandas.DataFrame.corr() –

What is Correlation ?

The correlation coefficient, also known as the Pearson product-moment correlation coefficient is a numerical index that reflects the relationship between two variables such as X and Y. It ranges in value from -1 to +1. If the two variables move in the same direction, then those variables are said to have a positive correlation. If they move in opposite directions, then they have a negative correlation.

How to create correlation Matrix in Pandas ?

Let’s read a dataset

import pandas as pd
from sklearn import datasets
housing = datasets.fetch_california_housing()
df = pd.DataFrame(, columns=housing.feature_names)

To create a correlation matrix in pandas, we can use df.corr() method.

corr_matrix = df.corr()

How to visualize correlation matrix in python –

To visualize correlation matrix in python, we can use matplotlib, seaborn or plotly.

Seaborn –

import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline


You can also annotate each cells using annot parameter.

sns.heatmap(corr_matrix, annot=True)

You can also change the colormap

sns.heatmap(corr_matrix, annot=True, cmap='YlGnBu')

Plotly Python –

import as px
fig = px.imshow(corr_matrix)

To annotate the cells use the text_auto parameter

fig = px.imshow(corr_matrix, text_auto=True)
fig.update_layout(height=600, width=700)

Rating: 1 out of 5.

Leave a Reply