How to execute commands in gnuplot using shell script?

What I want to do is write a script which first launches a program and then tells it to execute a bunch of commands and then quit. Lets go with an example.

I wrote this script myscript.sh and it doesn't work the way I want it to. What it does is just run gnuplot and wait for it to quit and then runs the other commands; which obviously produces errors.

#!/bin/bash
gnuplot
plot sin(x)
pause -1
quit

I guess it is clear what I'm trying to do; if not, then let me know in the comments.

0

6 Answers

One way is with -persist:

#!/usr/bin/gnuplot -persist
set title "Walt pedometer" font ",14" textcolor rgbcolor "royalblue"
set timefmt "%y/%m/%d"
set xdata time
set pointsize 1
set terminal wxt enhanced title "Walt's steps " persist raise
plot "/home/walt/var/Pedometer" using 1:2 with linespoints

another way, if you need to preprocess data, is with a Bash Here Document (see man bash):

#!/bin/bash
minval=0 # the result of some (omitted) calculation
maxval=4219 # ditto
gnuplot -persist <<-EOFMarker set title "Walt pedometer" font ",14" textcolor rgbcolor "royalblue" set timefmt "%y/%m/%d" set yrange $minval:$maxval set xdata time set pointsize 1 set terminal wxt enhanced title "Walt's steps " persist raise plot "/home/walt/var/Pedometer" using 1:2 with linespoints
EOFMarker
# rest of script, after gnuplot exits
2

From man gnuplot or its online manpage:

 -p, --persist lets plot windows survive after main gnuplot program exits. -e "command list" executes the requested commands before loading the next input file.

So what you probably want to run is the following command:

gnuplot -e "plot sin(x); pause -1"

Other variants I proposed but which are not that useful were:

gnuplot -p -e "plot sin(x); pause -1"
gnuplot -e "plot sin(x)"
gnuplot -p -e "plot sin(x)"
2

As explained in the man pages, gnuplot expects input from a command file in what is called an batch session. You can e.g. write the line plot sin(x) to a file myplot and then execute gnuplot myplot.

If you omit the command file, as your script does, you will get an interactive session.

4

This might help

{#set terminal postfile
{#set output "d1_plot.ps"
set title "Energy vs. Time for Sample Data"
set xlabel "Time"
set ylabel "Energy"
plot "d1.dat" with lines
pause -1 "Hit Enter to continue"

click here for more details

The here-doc method mentioned is highly useful with Gnuplot and with many other programs as well. By using shell variables within the Gnuplot commands in the here-doc, you can parameterize your plots with inputs from your shell script's command line. By cagily setting things up, you can mass-produce plots from vast troves of "big data." I used to produce consistent-looking scatter plots with 20,000 to 80,000 points PER PLOT in hundreds of structural dynamics finite analysis runs using exactly this method. It's a very powerful method.

1

gnuplot myscript.sh -

The trick is to put a dash '-' at the end of the command line.

It will leave the interactive gnuplot command alive. So that you can continue to insert command manually.

Everything after your quit command will not be evaluated.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like