在Gnuplot中循环统计名称

问题描述

我将数据存储在按块排列的文件中。我想将每个块中的所有数据作为单独的线以及该块的平均值绘制在同一图上。

我能够使用带有for循环的plot来显示数据

import sys
import os.path

parent_directory = os.path.split(os.path.dirname(__file__))[0]
if parent_directory not in sys.path:
    #sys.path.insert(0,parent_directory) # the first entry is directory of the running script,so maybe insert after that at index 1
    sys.append(parent_directory)

但是当我尝试使用与以下格式相同的统计信息

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

它给我一个错误提示for是一个未定义的变量。

我尝试在do for循环中编写统计信息,但是我需要为不同的块使用不同的名称。我尝试了两种方法

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

但这给我一个错误,说F是一个未定义的变量。第二种方法

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

但这只会绘制第一个块的数据,而将其他所有块都留在外面。

有更好的方法吗?

解决方法

如果您的gnuplot> = 5.2,那么我将统计值简单地放在一个数组中。对于较旧的gnuplot版本也将有解决方案。请注意,数组中的索引从1开始,而数据块中的索引从0开始。 像这样:

代码:

### 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

结果:

enter image description here