GNUPLOT:如何使用 CSV 文件中的标题行作为循环中的绘图标题?

Wol*_*fiG 2 plot gnuplot histogram columnheader

我有具有以下结构的 CSV 文件:

headerString1;headerString2;...;headerStringN
doubleNumber1;doubleNumber2;...;doubleNumberN
... many other doubleNumberRows
Run Code Online (Sandbox Code Playgroud)

我想从各个文件中的每一列绘制直方图 - 这是可行的 - 并且我想从 CSV 文件第一行获取每个单独图的标题。我搜索了很多,但能找到解决方案。到目前为止,这是我的 gnuplot 代码:

set datafile separator ";"
set style data histogram

binwidth=20
set boxwidth binwidth-2
bin(x,width)=width*floor(x/width)

# No legends!
unset key

do for [COL=1:10] {
    set title sprintf("%d", columnheader(COL)) <--- This always tells me it is a number, "%s" does not work
    FILE = sprintf("%s%02d%s","Histogram",COL,".png")
    set term png giant font helvetica 24 size 1440, 1080
    set output FILE
    plot "myCSVFile.CSV" using (bin(column(COL),binwidth)):(1.0) smooth freq with boxes lc 1
}
Run Code Online (Sandbox Code Playgroud)

columnheader(COL) 是一个数字(?),至少我可以通过sprintf("%d", columnheader(COL))将其转换为数字字符串,对于所有绘图,该数字字符串均为“-2147483648”。输出如下所示:

在此输入图像描述

如何检索 headerString# 字符串并将其用作我的个人图中的标题?

use*_*153 5

您只能在非常特定的上下文中(例如在命令中)访问列标题字符串plot。设置绘图标题不是其中之一(set title甚至不知道您将使用哪个数据文件),但创建图例条目是其中之一。因此,您可以将图例放置在标题通常出现的位置。

例如,给定数据文件test.csv

First Column;Second Column
-900;-700
-1100;-800
-1000;-650
Run Code Online (Sandbox Code Playgroud)

你可以用

set term push

set datafile separator ";"
set style data histogram
set style fill solid 1

binwidth=20
set boxwidth binwidth-2
bin(x,width)=width*floor(x/width)

set key outside top center samplen 0

do for [COL=1:2] {
    FILE = sprintf("%s%02d%s","Histogram",COL,".png")
    set term pngcairo
    set output FILE

    plot "test.csv" using (bin(column(COL),binwidth)):(1.0) smooth freq notitle with boxes lc 1, \
         NaN title columnhead(COL) with lines lc rgb "white"

    set output
}

set term pop
Run Code Online (Sandbox Code Playgroud)

并得到

在此输入图像描述 在此输入图像描述

在这里,我将显示直方图的图与生成图例条目的图分开,以便示例图片不会显示在图例中。

或者,如果您事先知道可能的列标题,您也可以使用

do for [name in '"First Column" "Second Column"'] {
   set title name
   plot "test.csv" using (bin(column(name),binwidth)):(1.0) smooth freq notitle with boxes lc 1
}
Run Code Online (Sandbox Code Playgroud)