【问题标题】:GNUPLOT: how to use a header row from CSV-file as plot header in a loop?GNUPLOT:如何在循环中使用 CSV 文件中的标题行作为绘图标题?
【发布时间】:2018-09-15 02:43:03
【问题描述】:

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

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

我想从 individual 文件中的每一列绘制直方图 - 这是可行的 - 我想从 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”对于所有地块。输出如下所示:

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

【问题讨论】:

  • 也许这个链接会有所帮助:stackoverflow.com/questions/36501353/…
  • 不,很遗憾没有。这个例子(就像我看到的其他例子一样)展示了如何使用列标题来标记连接到一个情节的系列。我想一个一个地处理这个系列,并将它们分别绘制到一个单独的文件中,图表的标题(而不是系列)由列标题给出。

标签: plot gnuplot histogram columnheader


【解决方案1】:

您只能在非常特定的上下文中访问列标题字符串,例如在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

得到

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

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

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
}

【讨论】:

  • 您好 user8153!感谢您的建议。不幸的是,我无法对其进行测试,因为我遇到了错误“您无法在多图模式下更改终端”,即使我在 set multiplot 之前将 set term 移动到循环之外 正如其他线程中所建议的那样。您是否尝试将代码与 set term 一起使用?
  • 您使用unset multiplot 退出多情节模式。你可以把它放在文件的开头,然后设置终端,然后切换到多图模式。但是,您在这里并不需要 multiplot。我编辑了答案,因此它创建了分隔数字,就像您在问题中所做的那样。
【解决方案2】:

我为我的问题找到了一个解决方法

我没有从文件中提取列标题(这是可取的),而是创建了一个标题数组,我必须从 csv 文件中复制它:( 代码:

titles = "columnHeader1 ... columnHeaderN"

do for [COL=1:N] {
    FILE = sprintf("%s%02d%s","Histogram",COL,".png")
    set term png giant font helvetica 24 size 1440, 1080
    set output FILE
    set title word(titles, COL)
    plot "InputFileName.CSV" using (bin(column(COL),binwidth)):(1.0) smooth freq with boxes lc 1
}

这可行,但比预期的要多点击几下...

【讨论】:

  • 你可以用titles = system("tr ';' ' ' &lt;InputFileName.CSV")替换你的第一行
猜你喜欢
  • 2013-04-24
  • 1970-01-01
  • 2020-09-02
  • 1970-01-01
  • 2015-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
相关资源
最近更新 更多