如何在 gnuplot 中对整个列求和?

How to sum an entire column in gnuplot?

我有多个这样的 CSV 文件(只有一列):

3
4
2.3
0.1

现在我想创建一个具有 <filename>:<sum of the column>.

的 gnuplot 条形图

但目前我很难总结一个专栏:

plot 'data1.txt' using 0:(sum [col = 0:MAXCOL] (col)) with linespoint;

您显示的命令是对每一行而不是每一列求和。

(1) 如果您可以在将 csv 文件提供给 gnuplot 之前转置 rows/columns,则此命令会生成接近您要求的绘图。请注意,MAXCOL 实际上是原始数据文件中的行数(不是列数)

  set boxwidth 0.5
  set style fill solid
  plot 'transpose_of_original' using 0:(sum [col=0:MAXCOL] col) with boxes

(2) 或者,您可以通过先累加总和然后再绘制来进行求和 gnuplot

  # get number of columns
  stats 'data1.txt' nooutput
  NCOL = STATS_columns
  array SUM[NCOL]
  # get sum for each column
  do for [col=1:NCOL] {
      stats 'data1.txt' using col nooutput
      SUM[col] = STATS_sum
  }

  # Now we plot the sums in a bar chart
  set style fill solid
  set boxwidth 0.5
  set xlabel "Column"
  set ylabel "Sum"
  plot SUM using 1:2 with boxes

在@Ethan 的帮助下,我解决了我的问题:

array files = ['data1.txt', 'data2.txt']
array SUM[|files|]
do for [i=1:|files|] {
  stats files[i] using 1 nooutput
  SUM[i] = STATS_sum
}
set style fill solid
set boxwidth 0.5
set xlabel 'File'
set ylabel 'Sum'
set yrange [0:]
plot SUM using 1:2:xticlabels(files[column(0)+1]) with boxes

data1.txt:

11
22
33
44

data2.txt:

11
2
33
4