【问题标题】:adding text ticklabels to pcolor heatmap将文本刻度标签添加到 pcolor 热图
【发布时间】:2016-01-04 00:00:36
【问题描述】:
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 制作回归热图。

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 代码如下:

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:]

【问题讨论】:

    标签: python matplotlib plot heatmap labels


    【解决方案1】:

    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()
    

    【讨论】:

      猜你喜欢
      • 2015-05-23
      • 2021-03-10
      • 2014-08-13
      • 2017-04-05
      • 1970-01-01
      • 2019-11-12
      • 2017-02-27
      • 2018-04-29
      • 1970-01-01
      相关资源
      最近更新 更多