#NEWLINE# def plot_csv( ) : #NEWLINE# #INDENT# 'Plot CSV data' #NEWLINE# parser = argparse . ArgumentParser ( description = 'Create plots from CSV data' , version = '{v}' . format ( v = __version__ ) ) #NEWLINE# parser . add_argument ( 'outfile' , action = 'store' , help = 'output file' ) #NEWLINE# parser . add_argument ( '-i' , '--infile' , type = argparse . FileType ( 'rb' ) , default = sys . stdin , help = 'input file (default: stdin)' ) #NEWLINE# parser . add_argument ( '--title' , action = 'store' , help = 'display the given title' ) #NEWLINE# parser . add_argument ( '--legend' , action = 'store_true' , default = False , help = 'display a legend' ) #NEWLINE# parser . add_argument ( '--grid' , action = 'store_true' , default = False , help = 'display a grid' ) #NEWLINE# parser . add_argument ( '--transparent' , action = 'store_true' , default = False , help = 'use a transparent canvas' ) #NEWLINE# parser . add_argument ( '--dpi' , action = 'store' , type = float , default = 80.0 , help = 'DPI for raster graphics (default: 80)' ) #NEWLINE# parser . add_argument ( '-x' , '--xcol' , type = int , metavar = 'X' , required = True , help = 'column number of data to be used for X axis (1-based)' ) #NEWLINE# parser . add_argument ( '--xlabel' , metavar = 'L' , help = 'label for X axis (default: name from column header)' ) #NEWLINE# parser . add_argument ( '--logX' , action = 'store_true' , default = False , help = 'use log scaling along the X axis' ) #NEWLINE# parser . add_argument ( '--xlim' , type = float , nargs = 2 , help = 'minimum and maximum X values to be displayed' ) #NEWLINE# parser . add_argument ( '-y' , '--ycols' , type = int , nargs = '+' , metavar = 'Y' , required = True , help = 'column number of data to be used for X axis (1-based)' ) #NEWLINE# parser . add_argument ( '--ylabel' , metavar = 'L' , help = 'label for Y axis (default: name from column header)' ) #NEWLINE# parser . add_argument ( '--logY' , action = 'store_true' , default = False , help = 'use log scaling along the Y axis' ) #NEWLINE# parser . add_argument ( '--ylim' , type = float , nargs = 2 , help = 'minimum and maximum Y values to be displayed' ) #NEWLINE# parser . add_argument ( '--labels' , nargs = '+' , help = 'labels to be used for plotted columns' ) #NEWLINE# cmd_args = parser . parse_args ( ) #NEWLINE# if ( cmd_args . labels and cmd_args . ycols and ( len ( cmd_args . labels ) != len ( cmd_args . ycols ) ) ) : #NEWLINE# #INDENT# print ( 'Error: number of labels ({nl}) must match number of columns for Y axis ({nc})' . format ( nl = len ( cmd_args . labels ) , nc = len ( cmd_args . ycols ) ) ) #NEWLINE# sys . exit ( 1 ) #NEWLINE# #UNINDENT# reader = csv . reader ( cmd_args . infile ) #NEWLINE# header = None #NEWLINE# rownum = 0 #NEWLINE# xlist = [ ] #NEWLINE# ylist = [ ] #NEWLINE# for row in reader : #NEWLINE# #INDENT# if ( len ( row ) == 0 ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# rownum += 1 #NEWLINE# if ( not is_numeric ( row [ 0 ] ) ) : #NEWLINE# #INDENT# if ( rownum == 1 ) : #NEWLINE# #INDENT# header = row #NEWLINE# #UNINDENT# continue #NEWLINE# #UNINDENT# row = map ( float , row ) #NEWLINE# xlist . append ( row [ ( cmd_args . xcol - 1 ) ] ) #NEWLINE# ylist . append ( [ row [ ( r - 1 ) ] for r in cmd_args . ycols ] ) #NEWLINE# #UNINDENT# cmd_args . infile . close ( ) #NEWLINE# xvals = np . array ( xlist ) #NEWLINE# yvals = np . array ( ylist ) #NEWLINE# if cmd_args . labels : #NEWLINE# #INDENT# labels = cmd_args . labels #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if header : #NEWLINE# #INDENT# labels = [ header [ ( i - 1 ) ] for i in cmd_args . ycols ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if cmd_args . legend : #NEWLINE# #INDENT# print ( 'Error: Must supply labels for legend (data has no header)' ) #NEWLINE# sys . exit ( 2 ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# fig = plt . figure ( ) #NEWLINE# ax = plt . subplot ( 1 , 1 , 1 ) #NEWLINE# if cmd_args . title : #NEWLINE# #INDENT# plt . title ( cmd_args . title ) #NEWLINE# #UNINDENT# if cmd_args . grid : #NEWLINE# #INDENT# ax . grid ( ) #NEWLINE# #UNINDENT# for c in range ( yvals . shape [ 1 ] ) : #NEWLINE# #INDENT# ax . plot ( xvals , yvals [ : , c ] , linestyle = 'solid' ) #NEWLINE# #UNINDENT# if cmd_args . xlim : #NEWLINE# #INDENT# ax . set_xlim ( [ cmd_args . xlim [ 0 ] , cmd_args . xlim [ 1 ] ] ) #NEWLINE# #UNINDENT# if cmd_args . ylim : #NEWLINE# #INDENT# ax . set_ylim ( [ cmd_args . ylim [ 0 ] , cmd_args . ylim [ 1 ] ] ) #NEWLINE# #UNINDENT# if cmd_args . logX : #NEWLINE# #INDENT# ax . set_xscale ( 'log' ) #NEWLINE# #UNINDENT# if cmd_args . logY : #NEWLINE# #INDENT# ax . set_yscale ( 'log' ) #NEWLINE# #UNINDENT# if cmd_args . xlabel : #NEWLINE# #INDENT# plt . xlabel ( cmd_args . xlabel ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if header : #NEWLINE# #INDENT# plt . xlabel ( header [ ( cmd_args . xcol - 1 ) ] ) #NEWLINE# #UNINDENT# #UNINDENT# if cmd_args . ylabel : #NEWLINE# #INDENT# plt . ylabel ( cmd_args . ylabel ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( len ( cmd_args . ycols ) == 1 ) : #NEWLINE# #INDENT# plt . ylabel ( header [ ( cmd_args . ycols [ 0 ] - 1 ) ] ) #NEWLINE# #UNINDENT# #UNINDENT# if cmd_args . legend : #NEWLINE# #INDENT# plt . legend ( labels ) #NEWLINE# #UNINDENT# try : #NEWLINE# #INDENT# plt . tight_layout ( ) #NEWLINE# #UNINDENT# except AttributeError : #NEWLINE# #INDENT# warnings . warn ( 'Matplotlib is out-of-date.  Consider upgrading.' , RuntimeWarning , stacklevel = 2 ) #NEWLINE# pass #NEWLINE# #UNINDENT# plt . savefig ( cmd_args . outfile , transparent = cmd_args . transparent , dpi = cmd_args . dpi )