10

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.

kxk7607
  • 207
  • 1
  • 4
  • 10

3 Answers3

6

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

Note that the explicit separator characters each count as exactly one separator. "4, 4" with set dataf sep ", " evaluates to "three columns, second is a missing value. If you have incompatible formats in one plot, you can import the data for each subplot with its own separators set using set table $<name>. (check "help datablocks")

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.

Karl
  • 2,117
  • 13
  • 26
  • Really? I get `extra chars after ` if I try to have more than one character as separators. – Benjamin W. Dec 29 '15 at 22:15
  • Are you using a rather old gnuplot version? – Karl Dec 30 '15 at 04:45
  • Hmmm, 4.6.4.2 it seems (Ubuntu package), and the latest version is 5.0 - might that be the reason? Can't test it at the moment. – Benjamin W. Dec 30 '15 at 05:01
  • 1
    Use of more than one separator was introduced sometime in the 4.6 release, i guess you just have a version preceding that change. Btw.: ubuntu has a gnuplot5 package if you want to try. – Karl Jan 01 '16 at 13:14
  • @Karl I don't succeed it with `", "`, neither with gnuplot 5.0 nor with gnuplot 5.4.1. To me it looks like although you can define several separation characters,, e.g. `",;|"`, but _not_ in combination with space. – theozh Jun 07 '22 at 18:31
  • @theozh Hm, the example I just added above works perfectly on my machine. If you have tabs instead of spaces in your data, you have to add "\t" to the character string. And don't try to c&p something with tabs into (w)gnuplot on windows, there is a known bug. – Karl Jun 07 '22 at 19:55
  • 1
    @Karl I guess I found it now. Your example works because you have **one** space. In my example I had multiple spaces at the beginning and multiple spaces as column separator. My suspicion is that if you specify `set datafile separator ", "` it will accept **exactly** one space as delimiter but not multiple spaces. Maybe you can try multiple spaces and confirm or falsify my suspicion. – theozh Aug 17 '22 at 19:05
  • @theozh Yes, that's right. It also doesn't accept "4, 4". The reason for that is clear imo: "3,,3" should always evaluate as "three columns, second column is a missing value". I see no way to. If you have incompatible separators in one plot, you need to first convert the datasets. csv and variable whitespace don't work together. – Karl Aug 19 '22 at 07:28
  • @Karl yes, only if you can be absolutely sure that you have always identical number of spaces, then you could plot e.g. ` 1 2` (two spaces each) as `plot $Data u 3:5`. – theozh Aug 19 '22 at 07:37
3

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
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • I ended up removing the commas from the .csv and inserting spaces. Does your recommendation create a new file from the .csv only without commas? I have never used `awk` before. – kxk7607 Jan 11 '13 at 23:21
  • @kxk7607 -- Basically. `awk` splits the file into columns based on the field (`-F,`) and then it prints out the requested columns. – mgilson Jan 12 '13 at 12:37
  • What if one column is a label with spaces and commas in it and my other data file is space separated and not suitable for awk piping? Ahhh, I'll just put double quotes around my labels according to this post: http://stackoverflow.com/questions/15013134/gnuplot-how-to-include-a-space-character-in-key-titles – tommy.carstensen Oct 15 '14 at 23:53
  • This answer is outdated. – Karl May 08 '16 at 01:51
0

Not just for "retro"-fun, but also for current gnuplot versions, I guess this is the only(?) gnuplot-only solution which works with all versions even with versions before the time of OP's question. The "trick" is: if you set datafile separator "," and read the first (and only) stringcolumn (of a whitespace separated file), i.e. strcol(1) will contain the full line. Now, you can simply split the string with word() and convert it into a floating point number with real().

If your original data has at least one space after the comma,

 1, -0.2
 2, -0.1
 3,  0.0

the data would be plotted correctly with keeping the separator as whitespace, since the comma after the first column's data will be ignored during number interpretation.

Although, for newer gnuplot versions (>=4.6.7, Apr 2015) you have the possibility to define several separators, however, which will not work as you might think, because

set datafile separator ", "

will interpret each space as column separator. So, if you have an undefined and variable number of spaces your plot command would fail.

Anyway, here is the "always" working solution:

Data:

SO14262760_1.dat (with variable number of spaces)

 1  -0.1
 2   0.0
 3  +0.1

SO14262760_2.dat (with no or some space after ,)

 1,-0.2
 2,-0.1
 3, 0.0

Script: (works with gnuplot>=4.4.0, March 2010)

### different column separators in two files with one plot command
reset

FILE1 = "SO14262760_1.dat"
FILE2 = "SO14262760_2.dat"

set datafile separator ","
myCol(n) = real(word(strcol(1),n))

plot FILE1 u (myCol(1)):(myCol(2)) w lp pt 7 lc rgb "red", \
     FILE2 u 1:2 w lp pt 7 lc rgb "blue"
### end of script

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72