12

I want to have a single .plt file storing both data and gnuplot commands. My data looks like

# x1 y1 x2 y2
  1  2  3  4
  5  6  7  8

and corresponds to two plots: (x1,y1) and (x2,y2).

I know I can use "-" like:

plot "-" using 1:2
# x1 y1 x2 y2
  1  2  3  4
  5  6  7  8
e

But that would generate only one plot, i.e., (x1,y1). I'm trying to do something like

plot "-" using 1:2, "-" using 3:4
# x1 y1 x2 y2
  1  2  3  4
  5  6  7  8
e

but obviously that doesn't work since gnuplot expects a new set of data from the standard input for the second "-".

Notes:

  1. I cannot change the style of the data. It comes in four columns.
  2. It seems that I can do it with reread but that requires two files. I really want only one file.
Lesmana
  • 25,663
  • 9
  • 82
  • 87
Mahdiyar
  • 335
  • 1
  • 3
  • 8

4 Answers4

7

You can't do this without modifying something about the way you input the data. When feeding gnuplot data via standard input, it expects multiple data sets to be delimited with two blank lines between them, or to be interleaved on successive lines. The options are:

  • Feed the two data sets into different plot commands altogether.

  • Change the file format so that data sets have blank lines between them, then reference them all with index.

  • Change the file format so that alternating lines represent different data sets, then reference them all with every.

  • Put the data into one file, the plotting script into another, and then reference the data file more than once with different using clauses each time.

There's an intro to the every and index commands starting at How do I plot several data sets in a single file? Those are the only facilities built into gnuplot for this sort of thing, and neither does exactly what you were asking about. it's good you've already modified the data formatting, because this wasn't ever going to work as you'd originally hoped.

David L.
  • 2,095
  • 23
  • 23
Greg Smith
  • 16,965
  • 1
  • 34
  • 27
  • Thanks. Good to know what can and what cannot be done (I wish this could be done though). Although I've changed my file, somehow the C++ code would generate the original format faster and I liked it more. Anyway, just one quick question: Apparently I can't combine `"-"` and `every`, right? – Mahdiyar Feb 15 '11 at 21:44
  • could you please give an example how to do this? gnuplot reads (while doing the first plot) to the end, no matter what i do. – flying sheep Feb 22 '11 at 13:21
4

New option since gp5.0 (see also help inline data) :

$dataset << EOD
1 2 3 4 
5 6 7 8
EOD

plot $dataset using 1:2, $dataset using 3:4
Karl
  • 2,117
  • 13
  • 26
4

I'm not sure how much you can edit the file, but the tidiest way is probably to put the whole thing in a shell script/batch script (are you on linux or windows?)

On linux I do something like this

#!/bin/bash

#put my data in a file
echo "
# x1 y1 x2 y2
  1  2  3  4
  5  6  7  8
" > my_dat.dat

#launch gnuplot
gnuplot<<EOF
#gnuplot commands here
set output "test.ps"
set term postscript

plot "my_dat.dat" u 1:2, \
     "my_dat.dat" u 3:4

set term pop
set output

EOF

# cleanup
rm my_dat.dat

Then I chmod +wrx the file I put the above commands in and run.

Note: there also seems to be a similarity to this question:

gnuplot stdin, how to plot two lines?

So you might want to look there too

Community
  • 1
  • 1
Tom
  • 5,219
  • 2
  • 29
  • 45
  • Thanks. Yes I'm on Linux. Actually I managed to change the way data was formatted by modifying the C++ code that had generated it. So my problem is gone now. But still I'm curious how one could do it such that I'd see the plot with one single file.plt in the above format and issuing `$ gnuplot file.plt`. – Mahdiyar Feb 14 '11 at 04:36
  • @Mahdiyar Yes, I'm curious about that too. Using gnuplot with scripting is the usual way I think (you can call your c++ program from the shell too making the whole thing work with 1 line), see - http://t16web.lanl.gov/Kawano/gnuplot/intro/working-e.html for more on this. – Tom Feb 14 '11 at 08:53
  • 2
    I like this shell script method, as I just want my app to generate a single plottable file. To keep the filesystem clean use the bash tempfile command: `tmpFile=\`tempfile\`` and then in your plot command use `plot "$tmpFile" 1:2 ...` – Rian Sanderson Jul 21 '11 at 00:48
  • @Rian, thanks, I hadn't seen that bash command before - very handy! – Tom Jul 21 '11 at 08:13
2

I know this is an old post, but I would like to point to another strategy in case someone else still struggles with this issue:

You could also use your plotting command and input the data twice, like:

plot "-" using 1:2, "-" using 3:4
# 1  2  3  4
  5  6  7  8
  e
  1  2  3  4
  5  6  7  8
  e

Gnuplot will actually wait for two blocks in this case. I find this very useful when I don't want to change the commands and when I am feeding Gnuplot by pipe. In a real-time scenario (depending on the data-size) this is very likely to be still faster than buffering to a file on the hard-drive.

In my experience the amount of code required to buffer the data in your script, which is required to pipe it more than once, is very low.

Martin R.
  • 1,554
  • 17
  • 16