Plot correlation matrix using pandas

0 votes
I am having difficulty analyzing the correlation matrix as the database has many features.  I want to plot a correlation matrix that we get using a dataframe.corr() function from the panda's library. How can I do this?
Apr 26, 2022 in Python by Kichu
• 19,040 points
2,162 views

1 answer to this question.

0 votes

Just use pyplot.matshow() from matplotlib:

import matplotlib.pyplot as plt

plt.matshow(dataframe.corr())

plt.show()

If df.corr() method ignores non-numerical columns, use .select_dtypes(['number']) when defining the x and y labels to avoid an unwanted shift of the labels.

Code : 

f = plt.figure(figsize=(19, 15))

plt.matshow(df.corr(), fignum=f.number)

plt.xticks(range(df.select_dtypes(['number']).shape[1]), df.select_dtypes(['number']).columns, fontsize=14, rotation=45)

plt.yticks(range(df.select_dtypes(['number']).shape[1]), df.select_dtypes(['number']).columns, fontsize=14)

cb = plt.colorbar()

cb.ax.tick_params(labelsize=14)

plt.title('Correlation Matrix', fontsize=16);

I hope this helps.

answered Apr 28, 2022 by narikkadan
• 63,600 points

Related Questions In Python

+2 votes
4 answers

How can I replace values with 'none' in a dataframe using pandas

Actually in later versions of pandas this ...READ MORE

answered Aug 13, 2018 in Python by bug_seeker
• 15,510 points
122,261 views
+2 votes
2 answers

How can I plot a k-dsitance graph using python?

Hi there, instead of sklearn you could ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,720 points
5,040 views
0 votes
1 answer

How to extract or split characters from number strings using Pandas?

You could just simply use a conversion ...READ MORE

answered Sep 18, 2018 in Python by aryya
• 7,460 points
2,049 views
+2 votes
1 answer

How can I record the X,Y limits of a displayed X,Y plot using the matplotlib show() module?

A couple hours after posting this question ...READ MORE

answered Dec 27, 2018 in Python by anonymous
1,134 views
+1 vote
1 answer

How to create plots using python matplotlib in IPython notebook?

I think you should try: I used %matplotlib inline in ...READ MORE

answered Aug 8, 2018 in Python by Priyaj
• 58,020 points
1,557 views
+1 vote
1 answer

How to handle Real-Time Matplotlib Plotting

To draw a continuous set of random ...READ MORE

answered Sep 26, 2018 in Python by Priyaj
• 58,020 points
16,384 views
0 votes
1 answer

How to increase plt.title font size?

Try the following : import matplotlib.pyplot as plt plt.figtext(.5,.9,'Temperature', ...READ MORE

answered Feb 11, 2019 in Python by SDeb
• 13,300 points
1,157 views
0 votes
1 answer

Detect and exclude outliers in a pandas DataFrame

Function definition. This handles data when non-numeric attributes ...READ MORE

answered Apr 28, 2022 in Python by narikkadan
• 63,600 points
3,720 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP