36

我的程序生成调用 gnuplot 的 bash 脚本。我不想制作额外的文件来存储数据;有什么方法可以显式调用所有值吗?或者可能让 bash 创建一个临时文件。

就像是

plot {(1,5),(2,10),(3,1)}

是我正在寻找的。

4

6 回答 6

46

您可以使用内联数据的语法 - 文件名'-'

以下示例在GIF图像(bash脚本)中生成一个简单的绘图:

gnuplot << EOF
set terminal gif
set output 'plot1.gif'
plot '-' using 1:2
        1 10
        2 20
        3 32
        4 40
        5 50
        e
EOF
于 2010-07-23T14:05:20.177 回答
27

Gnuplot 5.0.1 数据块

main.gnuplot

$data << EOD
1 1
2 4
3 9
4 16
EOD

plot "$data" \
  with linespoints \
  title "my data"

转换为 PNG:

gnuplot -e 'set terminal png' -e 'set output "main.png"' main.gnuplot

输出:

在此处输入图像描述

这种方法比'-'它更容易多次重复使用相同的数据,包括在相同的plot命令上更通用:How to embed multiple datasets in a gnuplot command script for a single plot command?

版本 5 在 Ubuntu 15.04 上可用,或从源代码编译:https ://askubuntu.com/a/684136/52975

使用函数绘图时,您可能还对+和特殊文件名感兴趣。++

在 Ubuntu 18.10、gnuplot 5.2 上测试。

于 2015-10-11T11:52:34.357 回答
4

将 shell 与管道一起使用的示例,

gnuplot -p <(echo -e 'plot "-"\n1 1\ne')
于 2012-06-13T04:08:01.703 回答
1

带有 gnuplot 的单线 ping 图

抱歉,它不轻(361 个字符):

gnuplot -p -e "set xdata time;set timefmt '%s';set xrange [ '$(date +%s)' : '$(date -d 'now +30 seconds' +%s)' ];plot '-' using 1:2 with line title 'ping google';" < <(( ping -c 30 -n google.com| sed -u 's/^64.*time=\([0-9.]\+\) .*$/\1/p;d' | tee >(sed -u 's/.*/now/'| stdbuf -oL date -f - +d%s)) | sed -u 'N;s/\n/ /;s/\([0-9.]\+\) d\([0-9]\+\) */\2 \1/;s/d//')

运行此行将使您的终端保持 30 秒,然后在屏幕上绘制一个图表,显示最后 30 秒对google.com的 ping 延迟。

同一行可以像这样拆分(也可行):

gnuplot -p -e "
    set xdata time;
    set timefmt '%s';
    set xrange [ '$(
        date +%s
      )' : '$(
        date -d 'now +30 seconds' +%s
      )' ];
    plot '-' using 1:2 with line title 'ping google';
    " < <((
    ping -c 30 -n google.com |
        sed -u 's/^64.*time=\([0-9.]\+\) .*$/\1/p;d' |
        tee >(sed -u 's/.*/now/'| stdbuf -oL date -f - +d%s)) |
    sed -u 'N;s/\n/ /;s/\([0-9.]\+\) d\([0-9]\+\) */\2 \1/;s/d//'
)

但这不打印值!

所以我添加了一些字节:

gnuplot -p -e "set xdata time;set timefmt '%s';set xrange [ '$(date +%s)' : '$(date -d 'now +30 seconds' +%s)' ];plot '-' using 1:2 with line title 'ping google';" < <(( ping -c 30 -n google.com| sed -u 's/^64.*time=\([0-9.]\+\) .*$/\1/p;d' | tee >(sed -u 's/.*/now/'| stdbuf -oL date -f - +d%s)  ) | sed -u 'N;s/\n/ /;s/\([0-9.]\+\) d\([0-9]\+\) */\2 \1/;s/d//' | tee >(printf "%(%T)T %s\n" $(</dev/stdin) | column -c $COLUMNS >&2 ))

这可能会创建一个像这样的窗口:

在此处输入图像描述

并同时在终端上打印:

17:58:53 19.6   17:59:00 124    17:59:07 159    17:59:13 194    17:59:19 17.1
17:58:54 18.7   17:59:02 19.4   17:59:08 20.3   17:59:14 16.8   17:59:20 20.0
17:58:55 17.9   17:59:03 180    17:59:09 76.4   17:59:15 48.9   17:59:21 192
17:58:57 115    17:59:04 186    17:59:10 226    17:59:16 221    17:59:22 17.1
17:58:58 18.5   17:59:05 16.8   17:59:11 109    17:59:17 19.0
17:58:59 17.0   17:59:06 184    17:59:12 18.8   17:59:18 18.7

重写拆分:

gnuplot -p -e "
    set xdata time;
    set timefmt '%s';
    set xrange [ '$(date +%s)' : '$(date -d 'now +30 seconds' +%s)' ];
    plot '-' using 1:2 with line title 'ping google';" < <(
  (
      ping -c 30 -n google.com |
        sed -u 's/^64.*time=\([0-9.]\+\) .*$/\1/p;d' |
        tee >(sed -u 's/.*/now/'| stdbuf -oL date -f - +d%s)
  ) | sed -u 'N;s/\n/ /;s/\([0-9.]\+\) d\([0-9]\+\) */\2 \1/;s/d//' |
  tee >(printf "%(%T)T %s\n" $(</dev/stdin) |
  column -c $COLUMNS >&2 )
)
于 2016-10-20T16:02:24.553 回答
0

解决方案是使用数组和参数线。

X="0.1 0.2 0.3 0.4 0.5"
Y="-1  0   2   4   8"

set parametric
set trange [1:words(X)]; set samples words(X)
plot (0+word(X,int(t))),(0+word(Y,int(t))) 

输出

来源:https ://groups.google.com/d/msg/comp.graphics.apps.gnuplot/UdiiC2cBQNo/xEyj6i7Y910J

于 2019-07-02T14:35:12.613 回答
0

10年后...

您可以直接在 gnuplot 中编写类似的内容:

plot "< ./format.sh '{(1,5),(2,10),(3,1)}'"

本地文件中的一个非常简单的脚本在哪里format.sh(记得给它 exec 权限):

#!/bin/bash
echo "$1" | sed 's:^..\(.*\)..$:\1:' | sed 's:),(:\n:g' | sed 's:,: :g'

它是如何工作的?

这三个sed命令执行以下操作:

  1. 分别删除第一个和最后两个字符,{(并且})
  2. 将序列更改),(为换行符
  3. 将剩余的更改,为空格

所以:

$ ./format.sh '{(1,5),(2,10),(3,1)}'
1 5
2 10
3 1

popen 上的 gnuplotplot "< command"语法杠杆在commandshell 中运行,捕获输出并动态绘制它。它在其他情况下也非常有用。

于 2020-09-23T10:58:53.117 回答