如何在热图中注释和正确放置数字

How to annotate and correctly place numbers in a heatmap

我在使用热图时遇到问题。

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

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,但是使用的数据具有更大的精度,因此会产生不同的颜色。

  • 使用起来更容易 seaborn.heatmap,其中包括注释和颜色栏。 seabornmatplotlib 的高级 API。
    • 这显着减少了代码行数。
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)

  • plt.txt 的 f 字符串不正确。 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)