Heat Map Annotation with text

1k views Asked by At

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 !!

Input

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 

Program 1 for only heatmap

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()

The below program is for including annotations

Program 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()

Error

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

'

1

There are 1 answers

5
tmdavison On

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