1
votes

I haven't used gnu plot before, but after googling around, I can't seem to figure out how to format my data file.

I have multiple datasets and each of these datasets has its own Yhigh and Ylow values. I wish to plot these 3 datasets on the same graph.

#X    Y          Yhigh     Ylow  
8    12181.0375  13198.4   11630.5
16   6096.362708 6500.86   4660.34
32   3086.159688 3282.08   2607.13
64   1668.613958 1963.03   1106.13
128  795.7439583 3929.95   554.027
192  486.0394774 614.181   362.243

I have 2 more datasets which I want to plot on the same graph. X values are the same. Y values different. However, the approximate Y-range will be similar (so the graph would not look that odd).

I read that, for multiple Y values, you can use:

#X    Y1    Y2    Y3

in your .dat file and then when plotting, separate as:

gnuplot> plot "test.dat" using 1:2 with lines,\
"test.dat" using 1:3 with lines

However, I am unsure how to do this when Y1 has Y1_high and Y1_low values, Y2 has Y2_high and Y2_low values, etc.

3

3 Answers

1
votes

The 'natural' formatting of a gnuplot data file would to separate the values with white spaces and without the braces:

X Y1 Y2 Y3

Now you just need

set style data lines
plot 'test.data' using 1:2, '' using 1:3

If you have commas as separators you must use

set datafile separator ','

to plot them correctly. Finally, using your format would work with an explicit format after the using statement:

plot 'test.dat' using 1:2 '(%lf,%lf,%lf,%lf)'

But that is not as flexible as it may seem, because the only allowed formatter is %lf.

1
votes

Christoph has suggested how you should reformat your data. You could do this using awk:

awk -F'[(),]' '{$1=$1}1' test.data > new.data

This takes your original data and splits it on parentheses and commas. $1=$1 forces awk to touch the data, so the format of the line is altered. The 1 at the end is just shorthand telling awk to print the line.

Your output then becomes:

 X     Y       Yhigh   Ylow   
 8 12181.0375 13198.4 11630.5 
 16 6096.362708 6500.86 4660.34 
 32 3086.159688 3282.08 2607.13 
 64 1668.613958 1963.03 1106.13 
 128 795.7439583 3929.95 554.027 
 192 486.0394774 614.181 362.243 

Your input can actually be altered using the same approach from within gnuplot:

plot "<awk -F'[(),]' '{$1=$1}1' test.data" every ::1

The every ::1 is used to skip the first line. Slightly shorter would be to swap the 1 at the end of the awk line for NR>1 which evaluates to 1 for every line after the first:

plot "<awk -F'[(),]' '{$1=$1}NR>1' test.data"
1
votes

I think perhaps my question was confusing. It was not about reformatting the dataset, but instead, I was asking how you plot multiple sets of data that have the same x-axis values.

I ended up solving my problem by following one of my proposed answers. Data of the format:

#X  Y1  Y1_high Y1_low  Y2 Y2_high Y2_low  Y3 Y3_high Y3_low

I was then able to plot lines and their associated error bars as follows:

plot "mydata.dat" using 1:2 with lines, "mydata.dat" using 1:2:3:4 with error bars,\
"mydata.dat" using 1:5 with lines, "mydata.dat" using 1:5:6:7 with error bars

[etc for Y3 values]