Search code examples
gnuplot

How to print input file next to graph in gnuplot?


Is it possible with gnuplot to print the data that I plotted next to the graph?

If I have a text file input.txt:

#x y
 1 2
 2 5
 3 6
 4 7

And I do plot 'input.txt' I'd like to have it plotted as usual and next to the plot I'd like to have the table printed. Is this possible?

Note: I'm on Windows and I'd like to format the output.


Solution

  • A bit late, but the OP asked for Windows... so, in short:

    data = system('type yourfile.dat')   # Windows
    

    In Windows, if you give a path, you need to pay attentention about \, spaces and doublequotes ".

    Data: SO22225051.dat

    #x y
     1 2
     2 5
     3 6
     4 7
    

    Script:

    Solution working for both Linux and Windows. Version 1 for gnuplot>=5.2.0, Version 2 for gnuplot>=4.6.0.

    ### place data as table/text in graph
    reset
    
    FILE = 'SO22225051.dat'
    set rmargin 15
    set label 1 at screen 0.9,0.7 font "Courier New,12"
    
    # Version 1: Windows & Linux using system() command; 
    # GPVAL_SYSNAME only available for gnuplot>=5.2.0
    
    getData(f) = GPVAL_SYSNAME[1:7] eq "Windows" ? \
                 system(sprintf('type "%s"',f)) : \
                 system(sprintf('cat  "%s"',f))     # Linux/MacOS
    
    Data = getData(FILE)
    set label 1 Data
    plot FILE u 1:2 w lp pt 7 lc rgb "red"
    
    pause -1
    
    # Version 2: gnuplot-only, platform-independent, working at least with gnuplot>=4.6.0
    
    Data = ''
    set datafile commentschar ''
    set datafile separator "\t"
    stats FILE u (Data=Data.strcol(1)."\n") nooutput
    set datafile commentschar   # restore default
    set datafile separator      # restore default
    
    set label 1 Data
    plot FILE u 1:2 w lp pt 7 lc rgb "red"
    ### end of script
    

    Result:

    The only difference between version 1 and 2 is that in version 2 gnuplot will remove leading spaces for each data line.

    enter image description here