【问题标题】:pipe plot data to gnuplot script管道图数据到 gnuplot 脚本
【发布时间】:2013-07-06 18:34:36
【问题描述】:

我想创建一个包含三个绘图的 gnuplot。 数据应该是内联的(我只想

应该是这样的:

目前我正在使用以下 gnuplot 脚本来创建绘图:

set terminal png
set output "test.png"
plot for[col=2:4] "data.txt" using 1:col title columnheader(col) with lines

文件data.txt是:

Generation Best Worst Average
0 2 1 0
1 3 1 2
2 4 3 3
3 4 3 3
4 6 3 4
5 7 4 5
6 9 6 7
7 10 6 9
8 10 5 6
9 11 6 8
10 12 7 9

我想将 data.txt 通过管道传输到 gnuplot 中,而不是依赖脚本中引用的数据文件。 像cat data.txt | gnuplot plot.gnu 这样的东西。 原因是,我有几个 data.txt 文件,不想为每个文件构建一个 plot.gnu 文件。

我读到了特殊的'-' 文件in this stackoverflow thread 和我读到了multiple plots in one file。但是,这需要将数据包含在不干净的 gnuplot 代码中。

【问题讨论】:

  • 可以打赌,你用基于人口的算法解决了一些优化问题;)

标签: gnuplot


【解决方案1】:

如果您使用的是 Unix 系统(即不是 Windows),您可以使用 '<cat' 而不是 '-' 从标准输入读取:

plot '<cat' using ...

那你就可以cat data.txt | gnuplot script.gp了。但是,在您在问题中提到的特定情况下,使用 for 循环中的绘图,您读取了输入三遍。所以通过stdin发送数据是不合适的,因为数据在第一次读取后就会消失。

【讨论】:

    【解决方案2】:

    不是直接的答案,但这是我用来快速查看数据的方法。 cut 命令特别有用

    cat data.txt | cut -f2 -d' ' | gnuplot -p -e "plot '<cat'"
    

    【讨论】:

    • 请注意,您将无法以任何方式操纵情节:例如拖动、调整大小——即使只是最大化窗口也会让 gnuplot 退出,因为数据不再可用。
    【解决方案3】:

    在 shell 中使用 gnuplot 的 -e 选项有什么问题? 您可以使用以下命令从 shell 提供一个变量作为输入,例如 data.txt:

    gnuplot -e "filename='data.txt';ofilename='test.png'" plot.gnu
    

    您应该能够使用 for 循环从 shell 中使用不同的“文件名”值多次调用上述命令。

    然后您将脚本 plot.gnu 更改为:

    set terminal png
    set output ofilename
    plot for[col=2:4] filename using 1:col title columnheader(col) with lines
    

    【讨论】:

      【解决方案4】:

      如果您想多次绘制来自管道的数据,则需要以某种方式将其存储在内存中。我首选的方法是使用/dev/shm 中的临时文件,该文件存在于大多数 Linux 系统中并映射到 RAM。为了保持干净,我设置了一个陷阱,在退出时删除临时文件。

      示例(使用您的 data.txt):

      cat data.txt | (cat > /dev/shm/mytempfile && trap 'rm /dev/shm/mytempfile' EXIT && gnuplot -e "set terminal dumb; plot for[col=2:4] '/dev/shm/mytempfile' using 1:col title columnheader(col) with lines")
      

      结果:

      12 ++------------+-------------+-------------+-------------+------------**
         +             +             +             +             + Best ****** +
         |                                                        Worst***#### |
      10 ++                                              *******Average $$$$$$++
         |                                           ****                      |
         |                                        ***    $$$$               $$$$
       8 ++                                     **     $$    $$        $$$$$  ++
         |                                    **     $$        $$    $$        |
         |                               *****    $$$              $$       ####
       6 ++                          ****       $$ ############# $$    #####  ++
         |                         **         $$ ##             #  ####        |
         |                       **        $$$ ##                ##            |
         |                     **      $$$$  ##                                |
       4 ++         ***********   $$$$$  ####                                 ++
         |     *****  ###################                                      |
         | ****   $$##                                                         |
       2 **    $$$##                                                          ++
         #########                                                             |
         + $$          +             +             +             +             +
       0 $$------------+-------------+-------------+-------------+------------++
         0             2             4             6             8             10
      

      【讨论】:

      • set terminal dumb 替换为set terminal dumb size $COLUMNS $LINES 更好,这会考虑当前终端大小并拉伸输出。
      【解决方案5】:

      使用system()命令怎么样

      set terminal png
      set output "test.png"
      
      # read shell input
      # the echo prints the variable, which is then piped to gnuplot
      fname = system("read filename; echo $filename")
      
      plot for[col=2:4] fname using 1:col title columnheader(col) with lines 
      

      你现在可以调用它

      echo "data.txt" | gnuplot script.gp
      

      【讨论】:

        【解决方案6】:

        混合两个答案:

        cat data.txt | gnuplot -e "set terminal png; set output "test.png"; plot for[col=2:4] '<cat' using 1:col title columnheader(col) with lines
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-27
        • 1970-01-01
        • 2014-01-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多