2015-10-06 87 views
0
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.  ]] 

我正在关注https://www.geekbooks.me/book/view/machine-learning-in-python以制作回归热图。向pcolor热图添加文本标签标签

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"] 
下面

迈克尔·鲍尔斯代码:

plt.pcolor(DF_correlation) 
plt.show() 

这工作得很好,但没有标签的地方,所以我尝试添加标签为matplotlib: colorbars and its text labels

我改变了格式一点点,但还是没有运气:

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

如何将标签添加到此图?这是一个相关性图,所以X和Y标签将是相同的...基本上headers[1:]

回答

3

代码从the answer you linked,运作良好。它看起来像你改变了一些意味着它不起作用的东西。

你拥有的主要问题是你想设置set_xticklabelsset_yticklabels到列表这里

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

然而,他们是Axes对象(ax)的方法,所以你必须给他们打电话,用headers列表作为参数。

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

下面是链接答案采用到脚本中的方法。我还旋转xticklabels阻止他们重叠(rotation=90),并且将它们移动到单元的中心(参见下面的set_xticksset_yticks线)

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