Changeset 6245


Ignore:
Timestamp:
Jan 30, 2009, 1:11:57 PM (10 years ago)
Author:
rwilson
Message:

Improved the 'call from code' interface.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • misc/tools/plotcsv/plotcsv.py

    r6238 r6245  
    11#!/usr/bin/env python
    22
    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
     5The code is written such that it may be run as a program or imported
     6and used as a module.
     7'''
    48
    59import sys
     
    913import time
    1014import getopt
    11 try:
    12     import pylab
    13 except:
    14     print 'Sorry, you have to install python-matplotlib'
    15     sys.exit(10)
     15import pylab
    1616
    1717
     
    2222Y_RANGE = 'y_range'
    2323FILENAME = 'filename'
     24SIZE = 'size'
    2425TITLE = 'title'
    2526X_LABEL = 'x_label'
     
    4142# @param options A dictionary of plot options.
    4243def plot_data(x_data, y_data, options):
    43     pylab.plot(x_data, y_data)  # x, y
     44    pylab.plot(x_data, y_data)
    4445    pylab.title(options.get(TITLE, ''))
    4546    pylab.grid(True)
     
    6667# @param options A dictionary of options.
    6768def 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
    6879    # get contents of data file
    6980    # after this, 'header' is list of column header strings
     
    8091    # convert column specifiers to 'int' if required
    8192    try:
    82         index = int(options[X_DATACOL])
     93        index = int(opts[X_DATACOL])
    8394    except:
    8495        try:
    85             index = header.index(options[X_DATACOL])
     96            index = header.index(opts[X_DATACOL])
    8697        except ValueError:
    87             error("Sorry, X column header '%s' isn't in the data file." % options[X_DATACOL])
    88     options[X_DATACOL] = index
     98            error("Sorry, X column header '%s' isn't in the data file." % opts[X_DATACOL])
     99    opts[X_DATACOL] = index
    89100
    90101    try:
    91         index = int(options[Y_DATACOL])
     102        index = int(opts[Y_DATACOL])
    92103    except:
    93104        try:
    94             index = header.index(options[Y_DATACOL])
     105            index = header.index(opts[Y_DATACOL])
    95106        except ValueError:
    96             error("Sorry, Y column header '%s' isn't in the data file." % options[Y_DATACOL])
    97     options[Y_DATACOL] = index
     107            error("Sorry, Y column header '%s' isn't in the data file." % opts[Y_DATACOL])
     108    opts[Y_DATACOL] = index
    98109   
    99110    # extract required columns from the data
    100     x_col = options[X_DATACOL]
    101     y_col = options[Y_DATACOL]
     111    x_col = opts[X_DATACOL]
     112    y_col = opts[Y_DATACOL]
    102113
    103114    # get max column number, check requested columns
     
    114125    y_label = header[y_col].title()
    115126
    116     options[TITLE] = 'File: %s' % filename
    117     options[X_LABEL] = x_label
    118     options[Y_LABEL] = y_label
     127    opts[TITLE] = 'File: %s' % filename
     128    opts[X_LABEL] = x_label
     129    opts[Y_LABEL] = y_label
    119130
    120     plot_data(x_data, y_data, options)
     131    plot_data(x_data, y_data, opts)
    121132
    122133
     
    131142    print '                   -v <range>   force a range in Y axis values, of the form'
    132143    print '                                    <min>,<max>, eq, "-5,10"'
    133     print '                   -h           prints this help'
     144    print '                   -s <min,max> sets PNG output picture size'
    134145    sys.exit(10)
    135146
     
    138149    of_dict = { '-x': X_DATACOL,
    139150                '-y': Y_DATACOL,
     151                '-s': SIZE,
    140152                '-v': Y_RANGE }
    141153
     
    150162    opt_dict = {}
    151163    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:')
    153165    if len(args) != 1:
    154166        usage()
     
    160172
    161173    # get name of data file
     174    if len(args) != 1:
     175        usage()
    162176    filename = args[0]
    163177    (filename_minus, _) = filename.split('.', 1)
     
    166180    opt_dict[X_DATACOL] = opt_dict.get(X_DATACOL, 0)
    167181    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)
    168184    opt_dict[Y_RANGE] = opt_dict.get(Y_RANGE, None)
    169185    opt_dict[FILENAME] = filename_minus
    170186
    171187    # 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.