本文介绍了如何在matplotlib中用文本注释热图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下方法在matplotlib中绘制热图:

I am plotting a heatmap in matplotlib using:

plt.pcolor(rand(5,5))

如何用绘制的实际数字注释热图?意思是在绘制的热图的每个单元格中,将与该单元格相对应的值放在传递给pcolor的5x5矩阵中.谢谢.

how can I annotate the heatmap with the actual numbers plotted? meaning in each cell of the plotted heatmap, put the value corresponding to that cell in the 5x5 matrix passed to pcolor. thanks.

推荐答案

没有自动功能可以执行此操作,但是您可以遍历每个点并将文本放置在适当的位置:

There is no automatic feature to do such a thing, but you could loop through each point and put text in the appropriate location:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(5, 4)
heatmap = plt.pcolor(data)

for y in range(data.shape[0]):
    for x in range(data.shape[1]):
        plt.text(x + 0.5, y + 0.5, '%.4f' % data[y, x],
                 horizontalalignment='center',
                 verticalalignment='center',
                 )

plt.colorbar(heatmap)

plt.show()

HTH

这篇关于如何在matplotlib中用文本注释热图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 05:14