-
Notifications
You must be signed in to change notification settings - Fork 2
/
plot.py
78 lines (66 loc) · 1.98 KB
/
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
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Routines for plotting prediction curves.
For usage information, call without any parameters.
Author: Jan Schlüter
"""
import sys
import os
if sys.version_info[0] > 2:
from io import BytesIO as StringIO
else:
try:
from cStringIO import StringIO
except:
from StringIO import StringIO
import numpy as np
import matplotlib; matplotlib.use('Agg')
import matplotlib.pyplot as plt
def pred_curve(data, ymax=1.0, format='png', outfile=None):
"""
Plots a prediction curve, either saved as a file or returned as a string.
"""
dpi = 96.
width = len(data)
height = 52.5
pad = 30.
plt.figure(figsize=((width + pad) / dpi, (height + pad) / dpi), dpi=dpi)
plt.axes((pad/(width+pad), .5*pad/(height+pad), width/(width+pad), height/(height+pad)))
plt.plot(data, color='#468CFF', lw=3)
plt.fill_between(np.arange(len(data)), 0, data.ravel(), color='#C9DEFF')
plt.xlim(0, len(data))
plt.ylim(0, ymax or data.max())
plt.xticks([])
if ymax is None:
pass
elif ymax==1:
plt.yticks([0, .5, 1])
elif ymax<.3:
plt.yticks([0, .1, .2])
elif ymax<.5:
plt.yticks([0, .2, .4])
f = outfile if outfile is not None else StringIO()
plt.savefig(f, format=format, transparent=True, dpi=dpi)
if outfile is None:
return f.getvalue()
def print_usage():
print('Plots a prediction curve.')
print('Usage: %s INFILE OUTFILE' % sys.argv[0])
print(' INFILE: .npy input file or a just number giving the length')
print(' OUTFILE: graphics file to write')
def main():
if len(sys.argv) < 3:
print_usage()
return
# 'parse' command line
infile, outfile = sys.argv[1:]
# read/generate input
if infile.endswith('.npy'):
data = np.load(infile)
else:
data = 0.02 * np.ones(int(infile))
# write output
pred_curve(data, format=None, outfile=outfile)
if __name__=="__main__":
main()