How to plot data without a separate file by specifying all points inside the Gnuplot script?

前端 未结 6 750
死守一世寂寞
死守一世寂寞 2020-12-02 20:19

My program generates bash scripts that call gnuplot. I don\'t want to have to make an extra file to store the data; is there any way I can explicitly call all of the values?

6条回答
  •  猫巷女王i
    2020-12-02 21:03

    10 years later...

    You can write something like this directly in gnuplot:

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

    where format.sh is a pretty simple script in a local file (remember to give it exec permissions):

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

    How does it works?

    The three sed commands do the following:

    1. drops the first and last two characters, respectively {( and })
    2. changes the sequence ),( to a newline
    3. changes the remaining , to spaces

    So:

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

    The gnuplot plot "< command" syntax levers on popen to run command in a shell, capture the output and plot it on the fly. It can be pretty useful also in other circumstances.

提交回复
热议问题