O.rka
O.rka

Reputation: 30687

adding text ticklabels to pcolor heatmap

DF_correlation = [[ 1.          0.98681158  0.82755361  0.92526117  0.89791366  0.9030177
   0.89770557  0.55671958]
 [ 0.98681158  1.          0.83368369  0.9254521   0.89316248  0.89972443
   0.90532978  0.57465985]
 [ 0.82755361  0.83368369  1.          0.81922077  0.77497229  0.7983193
   0.81733801  0.55746732]
 [ 0.92526117  0.9254521   0.81922077  1.          0.96940546  0.96637508
   0.95535544  0.54038968]
 [ 0.89791366  0.89316248  0.77497229  0.96940546  1.          0.93196132
   0.88261706  0.42088366]
 [ 0.9030177   0.89972443  0.7983193   0.96637508  0.93196132  1.
   0.90765632  0.50381925]
 [ 0.89770557  0.90532978  0.81733801  0.95535544  0.88261706  0.90765632
   1.          0.62757404]
 [ 0.55671958  0.57465985  0.55746732  0.54038968  0.42088366  0.50381925
   0.62757404  1.        ]]

I'm following https://www.geekbooks.me/book/view/machine-learning-in-python to make heatmaps of regressions.

import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt

headers = ["sex", "length","diameter", "height", "whole_weight", "shucked_weight","viscera_weight","shell_weight","rings"]

Michael Bowles code below:

plt.pcolor(DF_correlation)
plt.show()

This works fine but there are no labels so I tried adding labels as in matplotlib: colorbars and its text labels

I changed up the format a little bit but still no luck:

fig, ax = plt.subplots()
heatmap = ax.pcolor(DF_correlation)
cbar = plt.colorbar(heatmap)
ax.set_xticklabels = ax.set_yticklabels = headers[1:]
plt.show()

How can labels be added to this plot? It's a correlation plot so the x and y labels will be the same...basically headers[1:]

Upvotes: 3

Views: 8296

Answers (1)

tmdavison
tmdavison

Reputation: 69126

The code from the answer you linked, works well. It looks like you changed a few things which meant it didn't work.

The main problem you have is you are trying to set set_xticklabels and set_yticklabels to a list here

ax.set_xticklabels = ax.set_yticklabels = headers[1:]

However, they are methods of the Axes object (ax), so you have to call them, with the headers list as the argument.

ax.set_xticklabels(headers[1:])
ax.set_yticklabels(headers[1:])

Here's the methods from the linked answer adopted into your script. I also rotated the xticklabels to stop them overlapping (rotation=90), and moved them to the center of the cells (see the set_xticks and set_yticks lines below)

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

# Make DF_correlation into a DataFrame
DF_correlation = pd.DataFrame([
 [ 1.        ,  0.98681158,  0.82755361,  0.92526117,  0.89791366,  0.9030177 ,  0.89770557,  0.55671958],
 [ 0.98681158,  1.        ,  0.83368369,  0.9254521 ,  0.89316248,  0.89972443,  0.90532978,  0.57465985],
 [ 0.82755361,  0.83368369,  1.        ,  0.81922077,  0.77497229,  0.7983193 ,  0.81733801,  0.55746732],
 [ 0.92526117,  0.9254521 ,  0.81922077,  1.        ,  0.96940546,  0.96637508,  0.95535544,  0.54038968],
 [ 0.89791366,  0.89316248,  0.77497229,  0.96940546,  1.        ,  0.93196132,  0.88261706,  0.42088366],
 [ 0.9030177 ,  0.89972443,  0.7983193 ,  0.96637508,  0.93196132,  1.        ,  0.90765632,  0.50381925],
 [ 0.89770557,  0.90532978,  0.81733801,  0.95535544,  0.88261706,  0.90765632,  1.        ,  0.62757404],
 [ 0.55671958,  0.57465985,  0.55746732,  0.54038968,  0.42088366,  0.50381925,  0.62757404,  1.        ]
 ])

headers = ["sex", "length","diameter", "height", "whole_weight", "shucked_weight","viscera_weight","shell_weight","rings"]

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.25,left=0.25) # make room for labels

heatmap = ax.pcolor(DF_correlation)
cbar = plt.colorbar(heatmap)

# Set ticks in center of cells
ax.set_xticks(np.arange(DF_correlation.shape[1]) + 0.5, minor=False)
ax.set_yticks(np.arange(DF_correlation.shape[0]) + 0.5, minor=False)

# Rotate the xlabels. Set both x and y labels to headers[1:]
ax.set_xticklabels(headers[1:],rotation=90)
ax.set_yticklabels(headers[1:])

plt.show()

enter image description here

Upvotes: 9

Related Questions