From f6728b98a3002b9ea00281bc2479a4581321b886 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Fri, 21 Dec 2012 18:10:11 +0100 Subject: Add CSV plot script in python --- scripts/plotcsv.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 scripts/plotcsv.py diff --git a/scripts/plotcsv.py b/scripts/plotcsv.py new file mode 100755 index 0000000..32e96c5 --- /dev/null +++ b/scripts/plotcsv.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +import csv +import getopt +import os, sys +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] + + fig = plt.figure() + plt.plot(x, y) + 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() -- cgit v1.2.3-54-g00ecf