Reputation: 351
I have a correlation mat obtained from a data frame
>>> mat
Lcaud Rcaud Left_cereb_gm Right_cereb_gm Lamyg
Rcaud 0.931934 1.000000 0.856891 0.715523 0.924995
Left_cereb_gm 0.915274 0.856891 1.000000 0.938301 0.601521
Right_cereb_gm 0.744007 0.715523 0.938301 1.000000 0.445450
Lamyg 0.754676 0.924995 0.601521 0.445450 1.000000
Rput 0.717757 0.876985 0.635881 0.462773 0.912815
I can nicely plot it with :
heatmap = plt.pcolor(mat, cmap=matplotlib.cm.Blues)
but I would like to get the legends 'Lcaud', 'Rcaud' etc... on the plot.
How can I achieve this?
Upvotes: 0
Views: 2194
Reputation: 18521
I assume that you don't really want an actual legend since there is no easy way to define what color corresponds to each label or label-label pair. Instead, I think you are asking about setting the ticklabels? For example, given this data:
df = pd.DataFrame(np.random.rand(50, 5), columns=list('ABCDE'))
df.corr()
A B C D E
A 1.000000 0.184661 0.125002 0.024962 0.115385
B 0.184661 1.000000 0.224653 -0.178857 -0.185907
C 0.125002 0.224653 1.000000 -0.011785 0.238073
D 0.024962 -0.178857 -0.011785 1.000000 0.171649
E 0.115385 -0.185907 0.238073 0.171649 1.000000
we can do the following:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.pcolor(df.corr(), cmap=plt.cm.Blues)
ax.set_xticks(np.arange(5)+0.5) # center x ticks
ax.set_yticks(np.arange(5)+0.5) # center y ticks
ax.set_xticklabels(df.columns)
ax.set_yticklabels(df.columns)
Alternatively, Pandas has a helper function to help visualize the correlation among dataframe columns, called scatter_matrix
:
from pandas import scatter_matrix
scatter_matrix(df, diagonal='kde')
Upvotes: 2