summaryrefslogtreecommitdiff
path: root/scripts/plotcsv.py
blob: 931e4e8af2c22647481dc279b7b7a0988df690ec (plain)
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

import csv
import getopt
import os, sys
import scipy.interpolate
import numpy as np
import matplotlib.pyplot as plt

X_MAX = 100
Y_MAX = 50

def usage():
    print """usage: %s [OPTION...] CSV-FILE...""" % os.path.basename(sys.argv[0])

def read_and_plot_csv(csv_file, x_max=X_MAX, y_max=Y_MAX):
    reader = csv.reader(open(csv_file, 'r'), delimiter=',')
    x_name,y_name = reader.next() # header line

    X = np.array([[float(_x), float(_y)] for _x,_y in reader ])
    x = X[:,0]
    y = X[:,1]

    # interpolate data points
    xnew = np.linspace(min(x), max(x), len(x)*5)
    f = scipy.interpolate.interp1d(x, y)
    f2 = scipy.interpolate.interp1d(x, y, kind='cubic')

    fig = plt.figure()
    plt.plot(x, y, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--')
    plt.legend(['data', 'linear interp.', 'cubic interp.'], loc='best')
    plt.axis([0, x_max, 0, y_max], 'equal')
    plt.xlabel(x_name)
    plt.ylabel(y_name)
    plt.title(csv_file)
    plt.grid(True)

    plt.show()

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "h")
    except getopt.GetoptError, err:
        print(str(err))
        usage()
        sys.exit(-1)

    if len(args) < 1:
        usage()
        sys.exit(-1)

    for o, a in opts:
        if o == '-h':
            usage()
            sys.exit(0)
        else:
            assert False, "unhandled option"

    for csv in args:
        if not os.path.exists(csv):
            print "Error: File %s not found, skipping" % csv
            continue
        read_and_plot_csv(csv)

if __name__ == '__main__':
    main()