plot legends of a correlation matrix
<p>I have a correlation mat obtained from a data frame</p>

<pre><code>&gt;&gt;&gt; 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
</code></pre>

<p>I can nicely plot it with : </p>

<pre><code>heatmap = plt.pcolor(mat, cmap=matplotlib.cm.Blues)
</code></pre>

<p>but I would like to get the legends 'Lcaud', 'Rcaud' etc... on the plot. </p>

<p>How can I achieve this?</p>

<p>I assume that you don't really want an <a href="http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.legend" rel="nofollow">actual legend</a> 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:</p>

<pre><code>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
</code></pre>

<p>we can do the following:</p>

<pre><code>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)
</code></pre>

<p>which yields this:
<a href="http://i.stack.imgur.com/Dcjpl.png" rel="nofollow"><img src="http://i.stack.imgur.com/Dcjpl.png" alt="enter image description here"></a></p>

<p>Alternatively, Pandas has a helper function to help visualize the correlation among dataframe columns, called <a href="http://pandas.pydata.org/pandas-docs/stable/visualization.html#scatter-matrix-plot" rel="nofollow"><code>scatter_matrix</code></a>:</p>

<pre><code>from pandas import scatter_matrix
scatter_matrix(df, diagonal='kde')
</code></pre>

<p>yields
<a href="http://i.stack.imgur.com/6kWqv.png" rel="nofollow"><img src="http://i.stack.imgur.com/6kWqv.png" alt="enter image description here"></a></p>

python pandas matplotlib