2

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.

2 Answers 2

3

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'
Sign up to request clarification or add additional context in comments.

2 Comments

or echo $myarray | tr " " "\n" | gnuplot -p -e 'plot "<cat" w l'
Avoid useless use of cat! Use STDIN instead, see my answer
1

Plot STDIN

gnuplot -p -e 'plot "/dev/stdin"'

Sample:

( seq 5 10; seq 7 12 ) | gnuplot -p -e 'plot "/dev/stdin"'

or

gnuplot -p -e 'plot "/dev/stdin" with steps'  < <( seq 5 10; seq 7 12 )

More tunned plot

gnuplot -p -e "set terminal wxt 0 enhanced;set grid;
    set label \"Test demo with random values\" at 0.5,0 center;
    set yrange [ \"-1\" : \"80\" ] ; set timefmt \"%s\";
    plot \"/dev/stdin\" using 1:2 title \"RND%30+40\" with impulse;"  < <(

          paste <(
                seq 2300 2400
          ) <(
                for ((i=101;i--;)){ echo $[RANDOM%30+40];}
          )
)

Please note that this is still one line, you could Copy'n paste into any terminal console.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.