2

I am trying to plot some data with gnuplot. I want to use a static script.gp file and feed it from stdin with my data. I also have multiple datasets that I need to pass to the script.

script.gp:

set term jpeg
set encoding utf8
plot '<cat' index 0 with lines, plot '' index 1 with lines   

data:

1 2
2 2


0 0
7 4

command:

cat data | gnuplot script.gp

This doesn't work, since I'm guessing it tries to reread from stdin. Is there a way I can do this, or do I have to use temporary files to store my data?

2
  • if your data is already in a file, why don't you just plot the file? If the data is coming from a command, why don't you ask gnuplot to plot the output of the command? e.g. plot '< for i in {1..50}; do echo "$RANDOM"; done' w l maybe help special can give some hint for your case Commented Jan 25, 2016 at 15:11
  • Thanks for the reply. The data is not already in a file, it's generated on the fly (by a php script). My thought was to have a gnuplot script, version controlled and all, describing the format of the plots I want, and call that script with different datasets in order to generate the plots. Commented Jan 26, 2016 at 13:51

2 Answers 2

2

The solution I 've found so far has plenty of drawbacks, however it kind of does the job:
Use 'gnuplot -e' and cat the script file into the command:

cat data | gnuplot -e "$(cat script.gp)"

change script.gp using ; at the end of every line, remove all comments and change the plot command using '-' instead of '<cat' and removing index:

set term jpeg;
set encoding utf8;
plot '-' with lines, '-' with lines

change the data format, seperating each dataset with a line with e:

1 2
2 2
e
0 0
7 4
Sign up to request clarification or add additional context in comments.

1 Comment

This works great! Although I think in your second code block it should be plot '-' with lines, '-' with lines.
0

I would suggest to use the special file '<' which allows you to call the php script and you can have your gnuplot file plot.gp:

set term jpeg;
set encoding utf8;
set output file_out

my_cmd1=sprintf('< my_script %s', my_file1)
my_cmd2=sprintf('< my_script %s', my_file2)
plot my_cmd1 with lines, my_cmd2 with lines

and you call in a shell with:

gnuplot -e 'my_file1="input1.txt"; my_file2="input2.txt"; file_out="my_out.jpg"' plot.gp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.