12
votes

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.
4

4 Answers

7
votes

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.

4
votes

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

3
votes

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
1
votes

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.