Saurabh
Saurabh

Reputation: 67

Looping over stats name in Gnuplot

I have data in a file arranged in blocks. I want to plot all the data in each block as a separate line on the same plot along with the mean value for the block.

I was able to use plot with a for loop to display the data with

plot for [i=1:10] "F_vst.dat" every :::i::i u 1:2 w lines t i

But when I try to include stats using the same format as

stats for [i=1:10] ....

it gives me an error saying for is an undefined variable.

I tried writing stats in a do for loop, but I need different names for different blocks. I tried two methods:

do for [i=1:10] {
stats "F_vst.dat" every :::i::i u 2 name "F".i
}
plot for [i=1:10] "F_vst.dat" every :::i::i u 1:2 w lines t i, F.i_mean w dots

But this gives me an error saying F is an undefined variable. The second method:

do for [i=1:10] {
stats "F_vst.dat" every :::i::i u 2 name "F"
plot "F_vst.dat" every :::i::i u 1:2 w lines t i, F_mean w dots
}

But this plots data only for the first block and leaves every other block out.

Is there a better way to do this?

Upvotes: 1

Views: 416

Answers (1)

theozh
theozh

Reputation: 25714

If you have gnuplot >=5.2 then I would put the stats values simply in an array. There would be also solutions for older gnuplot versions. Note that indexing in arrays starts at 1, whereas indexing in datablocks starts at 0. Something like this:

Code:

### statistics in a loop
reset session

# create some test data
set print $Data
do for [i=1:10] {
    do for [j=1:20] {
        print sprintf("%g %g", j, rand(0)+i)
    } 
    print ""
}
set print

array F[10]
do for [i=1:10] {
    stats $Data u 2 every :::i-1::i-1 nooutput
    F[i] = STATS_mean
}
set key out Left

plot for [i=1:10] $Data u 1:2 every :::i-1::i-1 w lp pt 7 lc i notitle, \
     for [i=1:10] F[i] w l title sprintf("Mean% 3d: %g",i,F[i]) lc i
### end of code

Result:

enter image description here

Upvotes: 2

Related Questions