Search code examples
bashgnuplot

How can I have gnuplot 4.0 automatically use the top row entries as titles?


I found many solutions to this question for GNUPlot 4.2+ using title column, columheader etc. However, I am stuck with GNUPlot 4.0 and need to find a way to automatically set the titles using the top row of my data set.

I'm using a bash script to generate a dynamic (-> changes daily) file with data and I have a static .gnu file that contains the gnuplot settings.

Do you have any suggestions?

(Perhaps a way to use a variable/array inside the .gnu file?)


Solution

  • The trick is using the Command Substitution

    Sample Code:

    # content of file
    $ cat data.txt
    header
    1
    2
    3
    4
    5
    
    # generate gnuplot script
    $ echo "set term dumb; plot '<(tail -n +2 data.txt)' title '$(head -1 data.txt)'"
    set term dumb; plot '<(tail -n +2 data.txt)' title 'header'
    
    # pipe to gnuplot 
    $ echo "set term dumb; plot '<(tail -n +2 data.txt)' title '$(head -1 data.txt)'" | gnuplot
    
    # or you can use `here-doc` (note: `>` is bash prompt)
    $ gnuplot <<_EOF_
    >    set term dumb
    >    plot '<(tail -n +2 data.txt)' title '$(head -1 data.txt)'
    >_EOF_
    
        5 ++-------+--------+-------+--------+--------+--------+-------+-------+A
          +        +        +       +        +        +        +  header   A    +
      4.5 ++                                                                   ++
          |                                                                     |
          |                                                                     |
        4 ++                                                   A               ++
          |                                                                     |
      3.5 ++                                                                   ++
          |                                                                     |
          |                                                                     |
        3 ++                                 A                                 ++
          |                                                                     |
      2.5 ++                                                                   ++
          |                                                                     |
          |                                                                     |
        2 ++                A                                                  ++
          |                                                                     |
      1.5 ++                                                                   ++
          |                                                                     |
          +        +        +       +        +        +        +       +        +
        1 A+-------+--------+-------+--------+--------+--------+-------+-------++
          0       0.5       1      1.5       2       2.5       3      3.5       4
    

    Note: $(head -1 data.txt) expand to header before passing to Gnuplot.