5

I am writing a script to plot multiple files from multiple files one at a time. I also want to save the output of each plot corresponding to the data file. How can we give both arguments to GNUPlot. For example: sample GNUPlot script

set xlabel "a"
set ylabel "b"
set zlabel "c"
set term postscript
set output "??"    #'??' implies variable i.e. take file name from commandline
splot "??" with points,"??" with points    #'??' implies variable i.e. take file name from commandline

and this script will be run by another shell script generating the required file names.

Any help appreciated.

Aman Deep Gautam
  • 8,091
  • 21
  • 74
  • 130

2 Answers2

8

You can also use the -e command-line option of gnuplot. See this question for an example: How to pass command line argument to gnuplot?

Community
  • 1
  • 1
cabad
  • 4,555
  • 1
  • 20
  • 33
  • Did you find any official documentartion for the `-e` option? I'm searching for hours, still whithout luck. – Wolf Sep 19 '15 at 10:55
  • @Wolf You find it in the gnuplot docs the section "batch/interactive operation". – Christoph Sep 20 '15 at 07:55
  • @Christoph thanks, but it's still a little confusing: I found two versions of the same version's docs, [Batch/Interactive Operation](http://gnuplot.sourceforge.net/docs_4.2/node46.html) and [Batch/Interactive_Operation - Gnuplot: An Interactive Plotting Program](http://soc.if.usp.br/manual/gnuplot-doc/htmldocs/Batch_002fInteractive_005fOperation.html). It is so fundamental that I cannot believe why this information is hidden like this. – Wolf Sep 20 '15 at 09:23
  • 1
    @Wolf I agree that the info. should be easier to find. Luckily we have stackoverflow! As for the different documentations, they correspond to different gnuplot versions. – cabad Sep 21 '15 at 13:56
-2

Try this simple bash script

#!/bin/bash

file="output.png"
infile1="$1"
infile2="$2"

echo "
set xlabel \"a\"
set ylabel \"b\"
set zlabel \"c\"
set term postscript
set output \"$file\"    #'??' implies variable i.e. take file name from commandline
splot \"$infile1\" with points,\"$infile2\" with points    #'??' implies variable i.e. take file name from commandline
" > sample.gp && gnuplot sample.gp

And call it like ./script data.txt data2.txt You will have the output stored in output.png and the gnuplot file stored in sample.gp file.

It's easy to add more files to plot, even if the number of plotted files is different everytime. You can then also store your .gp file in different output.

bartimar
  • 3,374
  • 3
  • 30
  • 51