0

I would like to re-use the same stdin data for two or more lines in code like

plot '-' using 0:1 with lines, '' using 0:1:2 with yerrorbars
3.4 0.1
2.9 0.2
8.8 0.5
2.1 0.7

This code would work if instead of stdin I store all data into a file and then plot from this data file. However, plotting from stdin, the code behaves exactly like

plot '-' using 0:1 with lines, '-' using 0:1:2 with yerrorbars
3.4 0.1
2.9 0.2
8.8 0.5
2.1 0.7

(i.e. '-' instead of '') and gives a warning “Skipping data file with no valid points” instead of printing error bars to the line-plotted data.

Obviously, the plot can be made to work if I duplicate the data on stdin

plot '-' using 0:1 with lines, '' using 0:1:2 with yerrorbars
3.4 0.1
2.9 0.2
8.8 0.5
2.1 0.7
e
3.4 0.1
2.9 0.2
8.8 0.5
2.1 0.7

but I find this unsatisfactory, because then "" means the same thing as "-" for stdin. Of course, what I am looking for would require gnuplot to silently store the most recent data segment from stdin for potential re-use, which is probably in most cases not necessary. Still I do not think it is a huge burden on efficiency. So perhaps there is an option or a different use of gnuplot syntax that I do not know?

  • I do not know why I did not find them in the first search, but here are closely related questions: https://stackoverflow.com/questions/28921556/how-do-i-read-data-from-stdin-to-gnuplot-and-plot-same-data-twice and https://stackoverflow.com/questions/4585770/gnuplot-stdin-how-to-plot-two-lines in case anybody has the same problem. – Bernhard Bodenstorfer Oct 29 '19 at 08:27
  • You could combine stdin and [`datablock`](https://stackoverflow.com/a/58602984/11369382) with `table`. See `help plot with table`. – grsousajunior Oct 30 '19 at 11:56

1 Answers1

1

Why not using a datablock? I never use '-', exactly because of this issue. You can clear the data with undef $Data. Check help datablocks and help undef.

Code:

### re-use of data from a datablock 
reset session

$Data <<EOD
3.4 0.1
2.9 0.2
8.8 0.5
2.1 0.7
EOD

set xrange [-0.2:3.2]
plot $Data u 0:1 w l, '' using 0:1:2 w yerrorbars
### end of code

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72
  • Thanks, this is interesting. But can a datablock read from stdin, i.e. using a pipe? My simple coding examples may have misled a bit for simplicity of presentation; I would like to have the data from stdin. – Bernhard Bodenstorfer Oct 29 '19 at 07:54
  • The documentation you pointed to clearly says: If the special filename '-' appears in a plot command, then the lines immediately following the plot command are interpreted as inline data. See `special-filenames`. Data provided in this way can only be used once, by the plot command it follows. So I guess I am out of luck here. The feature would still be nice to have. – Bernhard Bodenstorfer Oct 29 '19 at 08:02