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

标签 python matplotlib seaborn 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 的单元格,以满足我所需的精度,但使用的数据具有更大的精度,会产生不同的颜色。

figure

最佳答案

  • 使用起来更方便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)

enter image description here

  • plt.txt 的 f 字符串不正确。对值进行四舍五入并将其转换为 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)

enter image description here

关于python - 如何在热图中注释和正确放置数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69667657/

相关文章:

matplotlib - 我如何适应长标题?

python - 循环内绘制的子图图表非常挤压

Seaborn diverging_palette 具有超过 2 种色调

python - 为什么会出现错误,KeyError : 'wsgi.input' ?

python - 制作 Pandas 系列的直方图

python - 使用 ccrs.epsg() 使用 EPSG 4326 坐标系绘制邮政编码周长形状文件

python - 在 Matplotlib 中创建箱线图均值线和中值线的键

python - mustache 被定义为 1.5* IQR,python seaborn boxplot 中的两个 mustache 怎么会不同呢?

python - scikit 学习中的样本权重和类权重选项有什么区别?

python - 从列表中随机选择项目而不重复