-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleHeatmap.py
More file actions
executable file
·75 lines (66 loc) · 2.31 KB
/
simpleHeatmap.py
File metadata and controls
executable file
·75 lines (66 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
import math
import sys
from matplotlib.ticker import MaxNLocator
from matplotlib.colors import BoundaryNorm
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
def random_point( r=1 ):
ct = 2*np.random.rand() - 1
st = np.sqrt( 1 - ct**2 )
phi = 2* np.pi * np.random.rand()
x = r * st * np.cos( phi)
y = r * st * np.sin( phi)
z = r * ct
return np.array( [x, y, z ] )
def near( p, pntList ):
(cnt,d0) = (0,1000)
for pj in pntList:
dist=np.linalg.norm( p - pj[0:2] )
if dist < d0:
d0 = dist
cnt = pj[2]
return cnt
def strip_first_col(fname, delimiter="\t"):
with open(fname, 'r') as fin:
next(fin) # skip header line
for line in fin:
try:
yield line.split(delimiter, 1)[1]
except IndexError:
continue
# create the numpy array of points from the file
lines = np.loadtxt(strip_first_col(sys.argv[1]))
pointList = np.zeros(shape=(np.shape(lines)[0],3))
for point in range(len(lines)):
pointList[point][0] = lines[point][0]
pointList[point][1] = lines[point][1]
pointList[point][2] = lines[point][8]
# generate a grid of points and initialize colormap
precision = 100
u = np.linspace( -np.pi, np.pi, precision*2 )
v = np.linspace( 0, np.pi, precision )
XX, YY = np.meshgrid(u, v, sparse=False, indexing='ij')
WW = np.zeros(shape=(np.shape(XX))) #this will be the colormap
# populate the heatmap with nearest crossing number
for i in range( len( XX ) ):
for j in range( len( XX[0] ) ):
x = XX[ i, j ]
y = YY[ i, j ]
WW[ i, j ] = near(np.array( [x, y] ), pointList)
# generate the plot
fg = plt.figure(figsize=(8,4))
ax = fg.add_subplot( 1, 1, 1 )
levels = MaxNLocator(nbins=64).tick_values(int(np.min(pointList[:,2])), int(np.max(pointList[:,2])))
cmap = plt.get_cmap('seismic')
norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)
im = ax.pcolormesh( XX, YY, WW, cmap=cmap, norm=norm)
plt.ylim(np.pi, 0)
plt.xlim(np.pi, -np.pi)
ax_divider = make_axes_locatable(ax)
cax = ax_divider.append_axes('top', size = '5%', pad = '11%')
fg.colorbar(im,cax=cax,orientation="horizontal")
fg.savefig('flat.png',dpi=300) # save the figure to file
plt.close(fg)