Changeset 6245 for misc/tools/plotcsv
- Timestamp:
- Jan 30, 2009, 1:11:57 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
misc/tools/plotcsv/plotcsv.py
r6238 r6245 1 1 #!/usr/bin/env python 2 2 3 '''A module to draw a graph on the screen given a data file.''' 3 '''A module to draw a graph on the screen given a data file. 4 5 The code is written such that it may be run as a program or imported 6 and used as a module. 7 ''' 4 8 5 9 import sys … … 9 13 import time 10 14 import getopt 11 try: 12 import pylab 13 except: 14 print 'Sorry, you have to install python-matplotlib' 15 sys.exit(10) 15 import pylab 16 16 17 17 … … 22 22 Y_RANGE = 'y_range' 23 23 FILENAME = 'filename' 24 SIZE = 'size' 24 25 TITLE = 'title' 25 26 X_LABEL = 'x_label' … … 41 42 # @param options A dictionary of plot options. 42 43 def plot_data(x_data, y_data, options): 43 pylab.plot(x_data, y_data) # x, y44 pylab.plot(x_data, y_data) 44 45 pylab.title(options.get(TITLE, '')) 45 46 pylab.grid(True) … … 66 67 # @param options A dictionary of options. 67 68 def plot_file(filename, options=None): 69 # set options defaults 70 opts = options 71 if opts is None: 72 opts = {} 73 74 opts[X_DATACOL] = opts.get(X_DATACOL, '0') 75 opts[Y_DATACOL] = opts.get(Y_DATACOL, '1') 76 opts[SIZE] = opts.get(SIZE, '800,600') 77 opts[Y_RANGE] = opts.get(Y_RANGE, None) 78 68 79 # get contents of data file 69 80 # after this, 'header' is list of column header strings … … 80 91 # convert column specifiers to 'int' if required 81 92 try: 82 index = int(opt ions[X_DATACOL])93 index = int(opts[X_DATACOL]) 83 94 except: 84 95 try: 85 index = header.index(opt ions[X_DATACOL])96 index = header.index(opts[X_DATACOL]) 86 97 except ValueError: 87 error("Sorry, X column header '%s' isn't in the data file." % opt ions[X_DATACOL])88 opt ions[X_DATACOL] = index98 error("Sorry, X column header '%s' isn't in the data file." % opts[X_DATACOL]) 99 opts[X_DATACOL] = index 89 100 90 101 try: 91 index = int(opt ions[Y_DATACOL])102 index = int(opts[Y_DATACOL]) 92 103 except: 93 104 try: 94 index = header.index(opt ions[Y_DATACOL])105 index = header.index(opts[Y_DATACOL]) 95 106 except ValueError: 96 error("Sorry, Y column header '%s' isn't in the data file." % opt ions[Y_DATACOL])97 opt ions[Y_DATACOL] = index107 error("Sorry, Y column header '%s' isn't in the data file." % opts[Y_DATACOL]) 108 opts[Y_DATACOL] = index 98 109 99 110 # extract required columns from the data 100 x_col = opt ions[X_DATACOL]101 y_col = opt ions[Y_DATACOL]111 x_col = opts[X_DATACOL] 112 y_col = opts[Y_DATACOL] 102 113 103 114 # get max column number, check requested columns … … 114 125 y_label = header[y_col].title() 115 126 116 opt ions[TITLE] = 'File: %s' % filename117 opt ions[X_LABEL] = x_label118 opt ions[Y_LABEL] = y_label127 opts[TITLE] = 'File: %s' % filename 128 opts[X_LABEL] = x_label 129 opts[Y_LABEL] = y_label 119 130 120 plot_data(x_data, y_data, opt ions)131 plot_data(x_data, y_data, opts) 121 132 122 133 … … 131 142 print ' -v <range> force a range in Y axis values, of the form' 132 143 print ' <min>,<max>, eq, "-5,10"' 133 print ' - h prints this help'144 print ' -s <min,max> sets PNG output picture size' 134 145 sys.exit(10) 135 146 … … 138 149 of_dict = { '-x': X_DATACOL, 139 150 '-y': Y_DATACOL, 151 '-s': SIZE, 140 152 '-v': Y_RANGE } 141 153 … … 150 162 opt_dict = {} 151 163 params = sys.argv[1:] 152 (opts, args) = getopt.gnu_getopt(params, 'x:y: v:h')164 (opts, args) = getopt.gnu_getopt(params, 'x:y:s:v:') 153 165 if len(args) != 1: 154 166 usage() … … 160 172 161 173 # get name of data file 174 if len(args) != 1: 175 usage() 162 176 filename = args[0] 163 177 (filename_minus, _) = filename.split('.', 1) … … 166 180 opt_dict[X_DATACOL] = opt_dict.get(X_DATACOL, 0) 167 181 opt_dict[Y_DATACOL] = opt_dict.get(Y_DATACOL, 1) 182 opt_dict[SIZE] = opt_dict.get(SIZE, '800,600') 183 # opt_dict[X_RANGE] = opt_dict.get(X_RANGE, None) 168 184 opt_dict[Y_RANGE] = opt_dict.get(Y_RANGE, None) 169 185 opt_dict[FILENAME] = filename_minus 170 186 171 187 # plot the file 172 plot_file( filename, opt_dict)188 plot_file(args[0], opt_dict)
Note: See TracChangeset
for help on using the changeset viewer.