35

我想创建一个包含三个图的 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

该文件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为每个文件构建一个文件。

我阅读了这个 stackoverflow 线程中'-'的特殊文件,并阅读了一个文件中的多个绘图。但是,这需要将数据包含在不干净的 gnuplot 代码中。

4

6 回答 6

30

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

plot '<cat' using ...

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

于 2013-07-10T16:58:29.057 回答
24

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

cat data.txt | cut -f2 -d' ' | gnuplot -p -e "plot '<cat'"
于 2015-06-26T18:57:23.137 回答
14

从 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
于 2013-07-09T12:12:49.893 回答
14

如果要多次绘制来自管道的数据,则需要以某种方式将其存储在内存中。我首选的方法是使用一个临时文件 in /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
于 2015-10-13T17:26:20.833 回答
3

如何使用 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
于 2014-01-07T14:20:33.543 回答
-2

混合两个答案:

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
于 2013-11-22T10:06:19.647 回答