如何计算 gnuplot 中一列的总和?

How to calculate a sum over a column in gnuplot?

对于文件:

#file1
1  3
2  2
3  6
4  5
5  1

我想用 gnuplot 绘制第二列的分布函数。所以我试图找到第 2 列中所有值的总和。我还没有找到解决这个问题的方法。你能给出一些建议来计算 gnuplot 中一列的总和吗?

我猜你想要一个滚动总和,作为一个独立的脚本

$ awk '{print [=10=],s+=}' file

1  3 3
2  2 5
3  6 11
4  5 16
5  1 17

使用 gnuplot,我猜语法是

plot "<awk '{print [=11=],s+=}' file"

只求总数

...   awk '{s+=} END{print s}' file

但是不确定您将如何绘制单个数字。

规范化列

...   awk 'NR==FNR{s+=; next} {print ,(s?/s:))' file file

不需要awk。检查 help stats.

stats "file1" u 2

print STATS_sum

完成后 stats 您可以输入 show var STATS 并查看更多已计算的变量。

加法: @jiadong,我之前说过,awk不需要规范化一个列。

代码:

### normalize a column
reset session

$Data <<EOD
#file1
1  3
2  2
3  6
4  5
5  1
EOD

stats $Data u 2 nooutput

plot $Data u 1:(/STATS_sum) w lp pt 7 ti sprintf("\nnormalized 2nd column\nTotal sum: %g",STATS_sum)

### end of code

结果: