0

I have a file where my data are separated into several indexes. I would like to plot some or all of the indexes as stacked filledcurves by adding the values of selected previous indexes to the values of the current index. I could not find a way to use the sum function as in the case of data arranged as columns in a single index (as in this question), even using the pseudocolumn(-2) as the index number.

Important note: every index as strictly identical sets of x values, only the y values differ.

Is there a way to do something like

p 'data.dat' index (sum(ind=1,3,4,5) ind) u 1:2 w filledcurve x1 t 'Sum(1,3,4,5)', '' index (sum(ind=1,2,5) ind) u 1:2 w filledcurve x1 t 'Sum(1,2,5)'

within gnuplot or do I have to resort to a script (maybe a variation of the one in this answer)?

Community
  • 1
  • 1
Jep
  • 59
  • 7
  • Please, always post a sample data file so that we can reproduce the problem and test the solution. – Miguel Mar 21 '16 at 12:25

2 Answers2

0

Although you may be able to do it using functions and variables of gnuplot 4.4+, this won't be very efficient as you want to perform an operation on several distant lines in your file, which is in fact an operation on arrays. Gnuplot is not meant for this, the datafiles should have a structure reasonably close to what you want to plot. I advise that you try to produce a file with such a structure, e.g. have the values you want to sum on the same line in different columns.

Joce
  • 2,220
  • 15
  • 26
  • I agree that gnuplot is not meant to deal with arrays. However I can't say before I have plotted them which of the indexes I shall keep for the final plot. The idea here is to not plot the indexes that do not contribute significantly to the total. Of course, I could plot each index separately, decide which ones to keep, generate a better formatted file and re-plot the "trimmed" data. If I do end up writing something doing just that I shall post it here. Thanks for your comment. – Jep Mar 21 '16 at 15:10
  • I'm not sure why it should be necessary to do two passes. You say your data is an array y=A(x, index). Rather than write A(:, i) for all i's (that is, a total of N tables of 2 columns), why not write A(:, :) ? (a single table of N+1 columns). Then gnuplot can handle what you want to do. – Joce Mar 23 '16 at 13:58
0

You can do this with some help outside gnuplot (invoked within gnuplot). Imagine you have the following data file with 4 indices (0 to 3):

1 2
2 3


1 5
2 5


1 0
2 3


1 4
2 3

Now say that we want to sum 1 and 2 and 0 and 3. The first sum should return:

1 5
2 8

while the second sum should return

1 6
2 6

We can select the blocks we want using set table:

set table "sum1"
plot for [i in "1 2"] "data3" index 0+i pt 7 not

set table "sum2"
plot for [i in "0 3"] "data3" index 0+i pt 7 not
unset table

Now use sed piping to remove the empty lines and smooth freq to sum for equal x values:

plot "< sed '/^\s*$/d' sum1" smooth freq t "sum1", \
     "< sed '/^\s*$/d' sum2" smooth freq t "sum2"

enter image description here

Miguel
  • 7,497
  • 2
  • 27
  • 46