2

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.

Foo Bar
  • 1,764
  • 4
  • 24
  • 43
  • How would you like to format the output? A minimal version of your script (including terminal settings) would be helpful. – andyras Mar 06 '14 at 15:13
  • I don't yet have a script (yet). I'd like to create plots like these: http://www.w-hanisch.de/assets/images/Los_Angeles.USA.jpg (the plot is no problem, I can do that, but getting the raw input formatted as a nice table next to the plot like in the picture is the problem because I don't know any commands that do that). Btw: the plot in the link is from a commercial program that can do only pixel pictures, I'd like to use gnuplot to create SVG to have a free and scaling alternative. – Foo Bar Mar 06 '14 at 15:51
  • That would probably be /possible/ but complicated, involving multiple parsing steps with external utils, multiple labels and drawing lines manually. I think it would not be worth the effort, and I suggest composing the figure as a PowerPoint slide and exporting it as a .png. – andyras Mar 06 '14 at 16:55

2 Answers2

1

Sure you can. The simplest way to do this in gnuplot is read in the file by calling an external command (cat on *nix, not sure on Windows) and storing the output as a variable, then setting a label on the graph. Here is how I do it:

set rmargin 8

datas = system('cat data.dat')
print datas

set label datas at graph 1.1,0.7

plot 'data.dat' notitle

This puts the data file off to the side, in place of a key.

enter image description here

andyras
  • 15,542
  • 6
  • 55
  • 77
  • Ah, I was hoping to be able to format the output somehow... And I'm on Windows. I don't know a cat command equivalent... – Foo Bar Mar 06 '14 at 13:49
  • And this question is already answered [here](http://stackoverflow.com/questions/206114/batch-files-how-to-read-a-file). Common case, looping through file entries using `FOR`. – Kamiccolo Mar 06 '14 at 16:08
1

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

theozh
  • 22,244
  • 5
  • 28
  • 72