csv2gnuplot.sh - HOWTO

csv2gnuplot.sh is a little script that transforms CSV files into data files suitable for gnuplot. In addition it can also produce gnuplot scripts that plot the data or output it to a certain format, instead of displaying it in a window. Another feature is batch-processing, which enables a user to easily process lots of files.

Requirements

  • Cygwin/Linux with bash
  • gnuplot
  • *nix tools: sed, head, wc, cat, gawk

Examples

  • Help ('-h')
    help can be displayed with the option -h
    usage: csv2gnuplot.sh [-i <file>] [-o <file>] [-g <file>] [-G <file>]
           [-O <file>] [-d <delim>] [-t] [-x] [-a] [-l] [-T]
           [-W <width> -H <height>]] [-F <x11|png|ps>]
           [-b <files>] [-e]
           [-h]
    
    Transforms a given CSV file into a gnuplot input file. It can also
    produce a gnuplot script for plotting the data, as well as batch
    processing of several files with automatic output generation.
    
     -h   this help
     -i   <file>
          the CSV file to use as input
     -o   <file>
          the gnuplot output file, output to stdout if not provided
     -g   <file>
          generates a gnuplot script with this name to display the data
          it assumes that the first column is the index for the x-axis.
          In combination with '-b' this parameter is only used to indicate
          that a script is wanted, the filename itself is ignored.
     -G   <file>
          a file containing gnuplot options, comments etc. to be added
          before the plots
     -O   <file>
          generates a script that outputs the plot in the format specified
          with '-F' in a file with the given name, instead of displaying
          it in a window
     -d   <delim>
          the delimiter that separates the columns, default: ,
     -t   transposes the matrix first
     -x   adds a column for the x-axis (numbers starting from 1)
     -a   generates the average of the columns
     -l   adds 'with lines' to the gnuplot script
     -T   adds a number as title to the gnuplot script
     -F   <x11|png|ps>
          the format of the output, default: x11
     -W   <width>
          the width of the output (if '-F png'), default: 800
     -H   <height>
          the height of the output (if '-F png'), default: 600
     -b   <files>
          processes the given files in batch mode, i.e. '-i' and '-o' are
          not necessary. the files get new extensions automatically.
          Note: use " if you're using wildcards like '*'
     -e   generates the desired output files directly, i.e. in creates a
          temp. gnuplot file and runs this (in combination with '-b',
          otherwise '-g' must be given).
          Works only if format is ps or png ('-F').
    

  • Transposing the file ('-t')
    In case that the data is row-wise and not column-wise, as gnuplot expects it to be, one can use the -t option to transpose the matrix. The following file
    1,2,3,4
    2,3,4,5
    4,5,6,7
    8,9,10,12
    then becomes this
    1 2 4 8
    2 3 5 9
    3 4 6 10
    4 5 7 12

  • Adding x-axis ('-x')
    If the data doesn't contain x-values, but only y-values - since it contains output of several runs of an algorithm - you can easily add an extra column that functions as x-axis. The option -x simply adds a number column starting with 1 to the data. Which transforms the file
    1,2,3,4
    2,3,4,5
    4,5,6,7
    8,9,10,12
    into this
    1 1 2 3 4
    2 2 3 4 5
    3 4 5 6 7
    4 8 9 10 12

  • Script generation ('-g')
    Instead of only producing the data file for gnuplot you can also generate a generic plot script to display all the data. This is done via the -g option.
    E.g. using our little example from above we can generate the data file and the script with this command (Note: we add the x-axis and transpose the file):
    csv2gnuplot.sh -i test.csv -o test.dat -g test.scr -tx
    This generates two files, 1. the data file test.dat and 2. the gnuplot script test.scr, which looks like this:
    # gnuplot script for 'test.dat'
    plot "test.dat" using 1:2
    replot "test.dat" using 1:3
    replot "test.dat" using 1:4
    replot "test.dat" using 1:5
    
    pause -1

  • Output options
    If one not only wants to display the single dots, but lines instead (-l) and the names of the plots as numbers (-T), one runs this command
    csv2gnuplot.sh -i test.csv -o test.dat -g test.scr -xtlT
    and gets this result



  • Format options ('-F')
    One can not only produce output that is plotted in a window, but also produce Postscript (-F ps) or PNG (-F png) files. In addition one has to add the name of the output file (-O ...). The previous command is modified like this
    csv2gnuplot.sh -i test.csv -o test.dat -g test.scr -xtlT -F png -O test.png
    produces this result



    By default an image with 800x600 is produced. The options -W and -H change the widht and height.

  • Batch processing
    Quite often one has to produce plots from a series of experiments, which is most of the time a tedious and boring task. The batch processing ability of csv2gnuplot.sh addresses this issue. E.g. one wants to generate Postscript files from all files that end with .out in the current directory, one can use the following command:
    csv2gnuplot.sh -xltT -b "*.out" -F ps -g dummy -e
    Note:
    • the value for -g is only to indicate that a gnuplot script should be generated.
    • For each file a data file with the extension .dat, a script file with .scr and an output file with .ps (since we produce Postscript) is produced.
    • The option -e only denotes that gnuplot should be run with the just generated script and data file.

Links

FracPete, 2005-05-21