gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 20:27:00 25 4
gpt4 key购买 nike

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

headerString1;headerString2;...;headerStringN
doubleNumber1;doubleNumber2;...;doubleNumberN
... many other doubleNumberRows

我想从单个文件中的每一列绘制直方图 - 这是可行的 - 并且我想从 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
}

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

enter image description here

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

最佳答案

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

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

First Column;Second Column
-900;-700
-1100;-800
-1000;-650

你可以使用

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

并得到

enter image description here enter image description here

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

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

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
}

关于plot - GNUPLOT:如何使用 CSV 文件中的标题行作为循环中的绘图标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49669603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com