Search code examples
arraysbashgnuplot

Gnuplot: How to plot bash array without dumping it to a file


I am trying to plot a bash array using gnuplot without dumping the array to a temporary file.

Let's say:

myarray=$(seq 1 5)

I tried the following:


myarray=$(seq 1 5)
gnuplot -p  <<< "plot $myarray"

I got the following error:

         line 0: warning: Cannot find or open file "1"
         line 0: No data in plot


gnuplot> 2
         ^
         line 0: invalid command


gnuplot> 3
         ^
         line 0: invalid command


gnuplot> 4
         ^
         line 0: invalid command


gnuplot> 5''
         ^
         line 0: invalid command

Why it doesn't interpret the array as a data block?

Any help is appreciated.


Solution

  • bash array

    myarray=$(seq 1 5)

    The myarray is not a bash array, it is a normal variable.

    The easiest is to put the data to stdin and plot <cat.

    seq 5 | gnuplot -p -e 'plot "<cat" w l'
    

    Or with your variable and with using a here-string:

    <<<"$myarray" gnuplot -p -e 'plot "<cat" w l'
    

    Or with your variable with redirection with echo or printf:

    printf "%s\n" "$myarray" | gnuplot -p -e 'plot "<cat" w l'
    

    And if you want to plot an actual array, just print it on separate lines and then pipe to gnuplot

    array=($(seq 5))
    printf "%s\n" "${array[@]}" | gnuplot -p -e 'plot "<cat" w l'