带文字的热图注释 [英] Heat Map Annotation with text

查看:188
本文介绍了带文字的热图注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制此热图. Col[2]Col[3]Col[1]xyz的轴.我想注释单元格.稍后,我尝试使用Col[0]作为轴上的标签(便于人类阅读).宝贵的建议表示赞赏.谢谢!!

I am trying to plot this heat map. Col[2], Col[3] and Col[1] are x, y and z axis. I want to annotate the cells. Later I am trying to use Col[0] as labels on axis (for human readable). Valuable suggestions are appreciated. Thanks !!

输入

0839d22dae7b 1952062 73.780274 19.990045 
a43cf52552c7 227842 73.780300 19.990028 
c9bb69ddffb8 1039483 73.780291 19.990085 
d25da834c045 1109172 73.780021 19.990063 
a4dbf9179594 586820 73.780103 19.990226 
b11c617da1d9 2793875 73.780131 19.990324 
31dc8159727a 1350028 73.780282 19.990195 
92bfaf82dbe1 579988 73.780214 19.990315 
46f9c8db4a74 2669870 73.780162 19.990332 

仅用于热图的程序1

import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt('input', unpack=True, dtype='str, float, float')

x  = data[1]
y  = data[2]

#hist = np.hist2d(x,y, bins=40)
plt.hist2d(x, y, bins=10)
#plt.pcolor(hist)
plt.colorbar()
plt.grid()
plt.show()

以下程序用于添加注释

程序2

import matplotlib.pyplot as plt
import numpy as np

data = np.loadtxt('inputfile', unpack=True, dtype='str, int, float, float')
heatmap = plt.pcolor(data)

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

plt.colorbar(heatmap)

plt.show()

错误

Traceback (most recent call last):
  File "heat4.py", line 5, in <module>
    heatmap = plt.pcolor(data)
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2928, in pcolor
    ret = ax.pcolor(*args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 7545, in pcolor
    X, Y, C = self._pcolorargs('pcolor', *args, allmatch=False)
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 7341, in _pcolorargs
    numRows, numCols = C.shape
AttributeError: 'list' object has no attribute 'shape

'

推荐答案

pcolor需要2D z数组,而只有1D数组.

pcolor requires a 2D z array, you only have a 1D array.

这篇关于带文字的热图注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆