管道绘图数据到gnuplot脚本

26 gnuplot

我想创建一个包含三个图的gnuplot.数据应该是内联的(正如我想要的那样)

它应该如下所示:

https://s22.postimg.cc/qcc94e1i9/test.png

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

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

该文件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
Run Code Online (Sandbox Code Playgroud)

我想将data.txt传递给gnuplot而不是依赖于脚本中引用的数据文件.有点像cat data.txt | gnuplot plot.gnu.原因是,我有几个data.txt文件,不想plot.gnu为每个文件构建一个文件.

我读到了这个stackoverflow线程中的特殊'-'文件,我在一个文件中读到了多个图.然而,这将需要包含gnuplot代码的数据,这是不干净的.

Dan*_*lke 25

如果您使用的是Unix系统(即非Windows),则可以使用'<cat'而不是'-'从stdin读取:

plot '<cat' using ...
Run Code Online (Sandbox Code Playgroud)

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


kir*_*gum 22

不是直接的答案,但这是我用来快速查看数据.它对cut命令特别有帮助

cat data.txt | cut -f2 -d' ' | gnuplot -p -e "plot '<cat'"
Run Code Online (Sandbox Code Playgroud)


Sid*_*idR 13

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

gnuplot -e "filename='data.txt';ofilename='test.png'" plot.gnu
Run Code Online (Sandbox Code Playgroud)

您应该能够使用for循环多次使用shell中"filename"的不同值调用上述命令.

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

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


Gia*_*lli 13

如果要多次绘制来自管道的数据,则需要以某种方式将其存储在内存中.我首选的方法是使用临时文件/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")
Run Code Online (Sandbox Code Playgroud)

结果:

12 ++------------+-------------+-------------+-------------+------------**
   +             +             +             +             + Best ****** +
   |                                                        Worst***#### |
10 ++                                              *******Average $$$$$$++
   |                                           ****                      |
   |                                        ***    $$$$               $$$$
 8 ++                                     **     $$    $$        $$$$$  ++
   |                                    **     $$        $$    $$        |
   |                               *****    $$$              $$       ####
 6 ++                          ****       $$ ############# $$    #####  ++
   |                         **         $$ ##             #  ####        |
   |                       **        $$$ ##                ##            |
   |                     **      $$$$  ##                                |
 4 ++         ***********   $$$$$  ####                                 ++
   |     *****  ###################                                      |
   | ****   $$##                                                         |
 2 **    $$$##                                                          ++
   #########                                                             |
   + $$          +             +             +             +             +
 0 $$------------+-------------+-------------+-------------+------------++
   0             2             4             6             8             10
Run Code Online (Sandbox Code Playgroud)

  • 更好的方法是将“setterminaldumb”替换为“setterminaldudusize$COLUMNS$LINES”,它会考虑当前终端大小并拉伸输出。 (2认同)