8
votes

I need to plot data from a .csv file and from a white space separated file. Both sets of data need to appear on the same plot.

data1.dat

 #t   y
  1   1
  2   1
  3   1

and

data2.csv

 #t,y
  1,2
  2,2
  3,2

normally I would do the following if both were .csv sets:

 set datafile separator ','
 plot 'data1.csv' using 1:2,'data2.csv' using 1:2

Is there some way to include the setting of the separation character in the plot statement?

plot 'data1.dat' using 1:2,'data2.csv' using datafile separator ',' using 1:2

The above does not work and I tried many different variations of the above code....I had no luck.

2

2 Answers

3
votes

AFAIK, there isn't a way to specify the separator. However, if you're in a POSIX compliant environment (and your gnuplot supports pipes -- which most do), you can farm the work out to awk pretty easily:

plot 'data1.dat' using 1:2,\
     "<awk -F, '{print $1,$2}' data2.csv" using 1:2
3
votes

You can give more than one character to set datafile separator, in your case ", ". All these are then individually treated as separators. (A tab caracter can be given as "\t", you need to put double quotes around the string then!)

$dat << EOD
1 2,4
2 2,5
3 1,6
4 4,4
EOD
set xr [0.5:4.5]
set dataf sep ", "
plot $dat us 1:2:3 w yerrorbars

If your data files have a very difficult format: gnuplots using specifier accepts a libC scanf() format string

plot "-" us 1:2 "%lf,%lf"
1,2
2,3
3,4
e

You can give a different format string for every file on your plot command. Note that gnuplot only accepts "double" fp numbers for input, so you have to use the %le or %lf specifier.

Check help using examples, and here is a full description of the format.