【问题标题】:gnuplot output filename from csv file来自csv文件的gnuplot输出文件名
【发布时间】:2012-12-21 19:22:24
【问题描述】:

我有一个 CSV 文件,我在 do for[] 循环中一一绘制列。我想将绘图保存为 PNG 文件,文件名来自列标题。将 text.png 替换为第 i 列标题的最佳方法是什么?

#!/bin/bash
set datafile separator ","
set key autotitle columnhead
set xlabel "time/date"
nc = "`awk -F, 'NR == 1{ print NF; exit}' input.csv`"
set term png

do for [i = 2:5] {
set output "test.png"
plot 'HiveLongrun.csv' every::0 using i:xticlabels(1) with lines
}

【问题讨论】:

    标签: csv gnuplot


    【解决方案1】:

    只要您使用 awk,您就可以再次使用它从 gnuplot 宏中获取标题名称:

    #!/usr/bin/env gnuplot
    
    set datafile separator ","
    set key autotitle columnhead
    set xlabel "time/date"
    nc = "`awk -F, 'NR == 1{ print NF; exit}' input.csv`"
    
    # Define a macro which, when evaluated, assigns the ith column header
    # to the variable 'head'
    awkhead(i) = "head = \"\`awk -F, 'NR == 1 {print $".i."}' input.csv\`\""
    
    set term png
    
    do for [i = 2:5] {
        eval awkhead(i)          # evaluate the macro
        set output head.".png"   # use the 'head' variable assigned by the macro
        plot 'HiveLongrun.csv' every::0 using i:xticlabels(1) with lines
    }
    

    几乎可以肯定,使用另一个类似 awk 的实用程序,甚至在 gnuplot 中,有一种更简洁的方法可以做到这一点。 Gnuplot 提供了几种运行任意内部/外部命令的方法,正如您从我的 backtics 和宏评估组合中看到的那样。

    顺便说一句,如果它可能会被 gnuplot 解释,那么在脚本开头有 bash shebang (#!/bin/bash) 对我来说有点奇怪。我假设您将其称为gnuplot myscript.plt。在这种情况下,shebang 只是一个注释(就 gnuplot 而言)并且没有做任何事情,因为 gnuplot 是解释器。在我的示例中,我使用 #!/usr/bin/env gnuplot,并将脚本作为 bash 中的可执行文件运行,例如 ./myscript.plt。在这种情况下,shebang 告诉 bash 使 gnuplot 成为解释器(或在命令提示符下键入 gnuplot 获得的任何命令)。当然,如果您不担心路径发生变化,您也可以将 shebang 设置为 #!/usr/bin/gnuplot

    【讨论】:

    • 打印 $".i."是什么给了我问题,我正在尝试 print $i
    猜你喜欢
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多