-
Notifications
You must be signed in to change notification settings - Fork 3
/
graph.py
61 lines (51 loc) · 1.42 KB
/
graph.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
import sys
import math
import collections
import argparse
ap = argparse.ArgumentParser()
ap.add_argument('-s', type=int, dest="s")
ap.add_argument('-x', type=int, dest="x", default=1)
ap.add_argument('-y', type=int, dest="y", default=2)
args = ap.parse_args()
class Stats:
def __init__(self):
self.s = 0
self.ss = 0
self.n = 0
def point(self, y):
self.s += y
self.ss += y**2
self.n += 1
def mean(self):
return self.s/self.n
def var(self):
return self.ss/self.n - (self.s/self.n)**2
def stdev(self):
return self.var() ** 0.5
data = collections.defaultdict(lambda: collections.defaultdict(Stats))
for line in sys.stdin:
fields = line.split()
if args.s:
s = int(fields[args.s-1])
else:
s = None
x = float(fields[args.x-1])
y = float(fields[args.y-1])
data[s][x].point(y)
print(r'\documentclass{standalone}')
print(r'\usepackage{tikz,pgfplots}')
print(r'\usepgfplotslibrary{fillbetween}')
print(r'\begin{document}')
print(r'\begin{tikzpicture}')
print(r' \begin{axis}')
for s in sorted(data):
print(r' \addplot coordinates {')
for x in sorted(data[s]):
ymean = data[s][x].mean()
print(rf' ({x},{ymean})')
print(r' };')
if s is not None:
print(rf' \addlegendentry{{{s}}}')
print(r' \end{axis}')
print(r'\end{tikzpicture}')
print(r'\end{document}')