Home > database >  How to title a gnuplot line based on offset array index in bash script?
How to title a gnuplot line based on offset array index in bash script?

Time:05-22

I have an array of names and a csv file:

  name=(abc def ghi jkl)    |     1, 2, 3, 4, 5
                            |     6, 7, 8, 9, 10
  

I want to plot the csv file by col1 against every other col(2..5), then title each line with the array. So the index of the column and the array to plot are not the same; offset. For instance:

 gnuplot < plot 'input.csv' using 1:2 t "${name[0]}" 

I've tried using the word() of gnuplot, and tried for (name, -1), (name, 1) as well:

 gnuplot < plot 'input.csv' using 1:2 t word(name, 0)

But I always receive the following error:

 line 0: internal error : non-STRING argument

I'm certain my array is space-separated, complying with word(). I don't know why it's not recognizing the names. Please and thank you for any suggestions to resolve...

CodePudding user response:

I looked through the gnuplot documentation and realize the word() of gnuplot only accepts a space-separated STRING, not array. So I converted the array accordingly, but in the gnuplot environment by persisting it. And account for the "offset", keeping in mind that the word index is 1-based:

     gnuplot -persist <<-EOFMarker
             nameList="${name[*]}"
             plot for [i=2:5] 'input.csv' using 1:i t word(nameList, i-1)
     EOFMarker

The graph is plotted as desired. I'm leaving the question up in case anyone encounters the same problem, though I'm sorry for asking a question and answering it myself. I hope it's not against StackOverflow rules...

  • Related