0

I have the following code, which plots 4 lines:

plot for [i=1:4] \
path_to_file using 1:(column(i)) , \

I also want to plot 8 horizontal lines on this graph, the values of which come from mydata.txt.

I have seen, from the answer to Gnuplot: How to load and display single numeric value from data file, that I can use the stats command to access the constant values I am interested in. I think I can access the cell (row, col) as follows:

stats 'mydata.txt' every ::row::row using col nooutput
value = int(STATS_min)

But their location is a function of i. So, inside the plot command, I want to add something like:

for [i=1:4] \
stats 'mydata.txt' every ::(1+i*10)::(1+i*10) using 1 nooutput
mean = int(STATS_min)
stats 'mydata.txt' every ::(1+i*10)::(1+i*10) using 2 nooutput
SE = int(STATS_min)
upper = mean + 2 * SE
lower = mean - 2 * SE

and then plot upper and lower, as horizontal lines on the graph, above.

I think I can plot them separately by typing plot upper, lower but how do I plot them on the graph, above, for all i?

Thank you.

Community
  • 1
  • 1
Shenan
  • 425
  • 2
  • 9
  • 20

1 Answers1

2

You can create an array and store the values in it, then using an index that refers to the value's position in the array you can access it inside a loop.

You can create the array as follows:

array=""
do for [i=1:4] {
val = i / 9.
array = sprintf("%s %g",array,val)
}

where I have stored 4 values: 1/9, 2/9, 3/9 and 4/9. In your case you would run stats and store your upper and/or lower variables. You can check what the array looks like in this way:

gnuplot> print array
 0.111111 0.222222 0.333333 0.444444

For plotting, you can access the different elements in the array using word(array,i), where i refers to the position. Since the array is a string, you need to convert it to float, which can be done multiplying by 1.:

plot for [i=1:4] 1.*word(array,i)

enter image description here

If you have values stored in a data file, you can process it with awk or even with gnuplot:

array = ""
plot for [i=1:4] "data" every ::i::i u (array=sprintf("%s %g",array,$1), 1/0), \
for [i=1:4] 1.*word(array,i)

The first plot instance creates the array from the first column data entries without plotting the points (the 1/0 option tells gnuplot to ignore them, so expect warning messages) and the second plot instance uses the values stored in array as variables (hence as horizontal lines in this case). Note that every takes 0 as the first entry, so [i=1:4] runs from the second through to the fifth lines of the file.

Miguel
  • 7,497
  • 2
  • 27
  • 46
  • Miguel, you suggest creating an array, by looping through i and using the stats command to create entries in that array, and then plotting elements of that array. The file, mydata.txt, comes from an Excel file, so I realise it is probably easier to create "upper" and "lower" within Excel. Let's assume I have all the values I want to plot in my file, called mydata.txt, in columns 1 and 2. Instead of looping through this to create my array, is it possible to create a 2-dimensional array directly from this file and then plot the values, using e.g. plot for [i=1:4] [j=1:2] 1.*word(array,i,j)? – Shenan Nov 09 '14 at 11:54
  • This is not *really* an array, but a string, from which individual elements separated by a space can be taken using `word()`, so instead of using a two dimensional array you would need to use two arrays (j=1,2) or map two indices onto one: k = i*(N-1) + j with N = number of entries. To read in values from a file and store them as variables in gnuplot you need to use an external utility, for example awk (plenty about that on this site). HOWEVER, if you already have the values you want to plot on a file there is nothing preventing you to plot them directly with the `every` keyword! Good luck! – Miguel Nov 09 '14 at 12:01
  • So, I want to go down the 'every' route, because the values are already in a file. I added 'for [i=1:4] path_to_expt every ::i::i using 1,' to the original script at the top of this post but it just plots 4 points, not horizontal lines. This is probably because I am using 'every' and 'using' at the same time, but I can't see how to plot horizontal lines on my graph. I think I will have to start a new post, because I imagine is should be easier than this and I probably haven't explained myself well enough! – Shenan Nov 09 '14 at 19:44
  • 1
    See my edit at the end of the answer, that should work for you. – Miguel Nov 09 '14 at 20:43