-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreq_plot.py
66 lines (49 loc) · 1.59 KB
/
freq_plot.py
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
#!/usr/bin/env python
'''
Ks type frequency histogram
prototype
'''
import sys
from pylab import *
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.path as path
# variables
bin_num=30
image_name = "hist.png"
fig = plt.figure(1, (8,8), dpi=300)
ax = axes([0.1,0.1,.8,.8])
fin=file(sys.argv[1]) # data in two-column tab delimited file
data=[[row.strip('\n').split('\t')[0] for row in fin]]
fin.seek(0)
data.append([row.strip('\n').split('\t')[1] for row in fin])
x=[int(i) for i in data[1]]
# calc frequencies
n, bins = np.histogram(x, bin_num)
nsum = float(sum(n))
n=map(lambda x: x/nsum, n)
# get the corners of the rectangles for the histogram
left = np.array(bins[:-1])
right = np.array(bins[1:])
bottom = np.zeros(len(left))
top = bottom + n
# we need a (numrects x numsides x 2) numpy array for the path helper
# function to build a compound path
XY = np.array([[left,left,right,right], [bottom,top,top,bottom]]).T
# get the Path object
barpath = path.Path.make_compound_path_from_polys(XY)
# make a patch out of it
patch = patches.PathPatch(barpath, facecolor='blue', edgecolor='gray', alpha=0.8)
ax.add_patch(patch)
# update the view
ax.set_xlim(left[0], right[-1])
ax.set_ylim(bottom.min(), top.max()+.05)
ax.set_xticks(np.arange(left[0], right[-1],int((-left[0]+right[-1])/bin_num*3)))
ax.set_xlabel('size')
ax.set_ylabel('frequency')
ax.set_title("Size distribution")
ax.grid(True)
##plt.show()
plt.savefig(image_name,dpi=300)
print >>sys.stderr, "print image to %s" % image_name