首页 > 解决方案 > 如何在热图中注释和正确放置数字

问题描述

我遇到热图问题。

我创建了以下函数来显示带有热图的分析

data = [ 0.00662896, -0.00213044, -0.00156812,  0.01450994, -0.00875174, -0.01561342, -0.00694762,  0.00476027,  0.00470659]

def plot_heatmap(pathOut, data, title, fileName, precis=2, show=False):
    from matplotlib import cm
    fig  = plt.figure()
    n       = int(np.sqrt(len(data)))
    data    = data.reshape(n,n)
    heatmap = plt.pcolor(data,cmap=cm.YlOrBr)
    xLabels = (np.linspace(1,n,n,dtype=int))
    yLabels = (np.linspace(1,n,n,dtype=int))
    xpos    = np.linspace(1,n,n)-0.5
    ypos    = np.linspace(1,n,n)-0.5

    for y in range(n):
        for x in range(n):
            plt.text(x + 0.5, y + 0.5, f'{data[y, x]:.{precis}f}',
                horizontalalignment='center',
                verticalalignment='center',
                )

    plt.colorbar(heatmap, format='%.2f')
    plt.xticks(xpos,xLabels)
    plt.yticks(ypos,yLabels)
    plt.title(f'{title}')
    if (show == False ):
        plt.close(fig)        
    elif (show == True):        
        plt.show()    
    fig.savefig(f'{pathOut}/{fileName}.pdf', format='pdf')   

当我调用该函数时,会创建热图但不正确,因为我想以特定精度显示值。我知道如何定义文本精度和比例精度,但如何调整数据精度以生成正确的热图?

在附图中,对于我想要的精度,我有 7 个等于 0 的单元格,但是使用的数据具有更大的精度,会产生不同的颜色。

数字

标签: pythonmatplotlibseabornheatmap

解决方案


  • 它更容易使用seaborn.heatmap,其中包括注释和颜色条。seabornmatplotlib.
    • 这显着减少了代码行数。
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
import seaborn as sns

def plot_heatmap(pathOut, fileName, data, title, precis=2, show=False):
    n = int(np.sqrt(len(data)))
    data = data.reshape(n, n)
    
    xy_labels = range(1, n+1)
    
    fig, ax = plt.subplots(figsize=(8, 6))
    p = sns.heatmap(data=data, annot=True, fmt=f'.{precis}g', ax=ax,
                    cmap=cm.YlOrBr, xticklabels=xy_labels, yticklabels=xy_labels)

    ax.invert_yaxis()  # invert the axis if desired
    ax.set_title(f'{title}')
    fig.savefig(f'{pathOut}/{fileName}.pdf', format='pdf') 
    if (show == False ):
        plt.close(fig)        
    elif (show == True):        
        plt.show()


data = np.array([ 0.00662896, -0.00213044, -0.00156812,  0.01450994, -0.00875174, -0.01561342, -0.00694762,  0.00476027,  0.00470659])

plot_heatmap('.', 'test', data, 'test', 4, True)

在此处输入图像描述

  • 的 f 字符串plt.txt不正确。round将值转换为类型会更容易str
    • str(round(data[x, y], precis))代替f'{data[y, x]:.{precis}f}'
  • data[x, y]应该data[y, x]
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np

def plot_heatmap(pathOut, fileName, data, title, precis=2, show=False):
    fig  = plt.figure(figsize=(8, 6))
    n       = int(np.sqrt(len(data)))
    data    = data.reshape(n, n)
    heatmap = plt.pcolor(data, cmap=cm.YlOrBr)
    xLabels = (np.linspace(1,n,n,dtype=int))
    yLabels = (np.linspace(1,n,n,dtype=int))
    xpos    = np.linspace(1,n,n)-0.5
    ypos    = np.linspace(1,n,n)-0.5

    for y in range(n):
        for x in range(n):
            s = str(round(data[y, x], precis))  # added s for plt.txt and reverse x and y for data addressing
            plt.text(x + 0.5, y + 0.5, s,
                horizontalalignment='center',
                verticalalignment='center',
                )

    plt.colorbar(heatmap, format=f'%.{precis}f')  # add precis to the colorbar
    plt.xticks(xpos,xLabels)
    plt.yticks(ypos,yLabels)
    plt.title(f'{title}')
    fig.savefig(f'{pathOut}/{fileName}.pdf', format='pdf')  # this should be before plt.show()
    if (show == False ):
        plt.close(fig)        
    elif (show == True):        
        plt.show()


# the function expects an array, not a list
data = np.array([ 0.00662896, -0.00213044, -0.00156812,  0.01450994, -0.00875174, -0.01561342, -0.00694762,  0.00476027,  0.00470659])

# function call
plot_heatmap('.', 'test', data, 'test', 4, True)

在此处输入图像描述


推荐阅读