Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to integrate gnuplot files with a shell script?

19 views
Skip to first unread message

hiankun

unread,
Apr 23, 2009, 4:52:09 AM4/23/09
to
Hi, I am writing a shell script to work with gnuplot. So far, it works
with some basic functions I expected, but I want to modified it to be
more ``reusable.''

Here is my original script named ``plot_all.sh''
------------------------------------------------------------------------
#!/bin/bash

usage="\tVersion: 0.1
\tUsage: ./plot_all.sh file.plt [terminal]
\tfile.plt = file contains data to be plotted
\tterminal = the output type; if ignored, 'x11' is set
\t\tavailable values are: png, eps, pdf"

if [ $# -lt 1 ]; then
echo -e "$usage"
exit 0
fi

filename=`basename $1 .plt`
ext=$2

options=""
outputs="set output \"$filename.$ext\""

case "$ext" in
"png")
terminal="png"
options='font "/usr/share/fonts/truetype/freefont/FreeSans.ttf"';;
"eps" | "pdf")
terminal="post eps color"
ext="eps"
outputs="set output \"$filename.$ext\"";;
"svg")
terminal="svg";;
*)
terminal="x11"
outputs="";;
esac

gnuplot -persist << EOF
set term $terminal enhanced $options
$outputs
set xtics nomirror
set ytics nomirror
set title "Acceleration record [$filename]"
set xlabel "time (sec.)"
set ylabel "accleration (m/s^2)"
set yrange[-4:4]
plot "$filename.plt" u 1:2 t "a_x" w lines lt 1,\
"" u 1:3 t "a_y" w lines lt 2,\
"" u 1:4 t "a_z" w lines lt 3
EOF

if [ "$2" == "pdf" ]; then
epstopdf $filename.$ext
echo -e "The $filename.eps has been created\nand then be converted
to $filename.pdf\nby using epstopdf.\n"
fi

if [ "$terminal" == "x11" ]; then
echo -e "The terminal has been set as x11\nand no plot has been
outputed.\n"
fi
------------------------------------------------------------------------------------

And what I am trying is to make the part of gnuplot settings (that is,
lines between `gnuplot -persist << EOF' and `EOF' of the previous
script) ``replaceable.'' For example, I probably have several
different gnuplot settings for different data sets, and these settings
are saved as plain text files individually. If my script (plot_all.sh)
can read the different settings into itself to ``replace'' the
original setting part and execute these setting as they have been
written in the script, then I can use the same script repeatedly.

Is it possible for the shell script to perform this kind of work?
Because I am very new in writing shell script, it's likely I have
thought the whole problem in the wrong way. I have tried to find
commands in books but get no clear idea yet, so I post my question
here and hope someone could give me some advices. Thank you in advance.

Maxwell Lol

unread,
Apr 23, 2009, 7:39:15 AM4/23/09
to
hiankun <hia...@gmail.com> writes:

> Hi, I am writing a shell script to work with gnuplot. So far, it works
> with some basic functions I expected, but I want to modified it to be
> more ``reusable.''

By the way - I have had great experience with the 2D plot program
called grace. (formerly Xmgr and Ace/gr)

I usually use the following

1) Create a data file

2) Run grace on that file. It displayes the graph on the screen.

3) Interactively add labels, axis, comments, titles, legends, color,
line style, plot style, etc. Grace is extremely flexible here, and
since it's interative, you see the results immediately..

4) Save the parameter file

5) generate one or more new data files

6) for each data file, run grace with the new data file, and the old
parameter file.

7) tweak the plot if necessary. Output each one in EPS, GIF, etc. format.

http://plasma-gate.weizmann.ac.il/Grace

The UI is a little clunky, but the flexibility is great. It's also scriptable.


hiankun

unread,
Apr 23, 2009, 9:25:18 PM4/23/09
to
On Apr 23, 7:39 pm, Maxwell Lol <nos...@com.invalid> wrote:

> hiankun <hian...@gmail.com> writes:
> > Hi, I am writing a shell script to work with gnuplot. So far, it works
> > with some basic functions I expected, but I want to modified it to be
> > more ``reusable.''
>
> By the way - I have had great experience with the 2D plot program
> called grace.  (formerly Xmgr and Ace/gr)
>
(oops, I think I mistook the meaning of `reply to author' and sent the
reply to your email...)

Thank you for this useful information. The functions of Grace looks
great and I will give it a try.
(And of course, I still want to know whether there is solution for my
initial script problem.)

Jon LaBadie

unread,
Apr 24, 2009, 2:18:47 AM4/24/09
to


SettingsFile=<you set it to some default, cmd line arg, or ???>

gnuplot -persist << EOF
$(cat $SettingFile)
EOF

hiankun

unread,
Apr 26, 2009, 9:47:15 PM4/26/09
to
On Apr 24, 2:18 pm, Jon LaBadie <jXlaba...@aXcm.oXrg> wrote:
>
> SettingsFile=<you set it to some default, cmd line arg, or ???>
>
> gnuplot -persist << EOF
> $(cat $SettingFile)
> EOF

Hi, Jon LabBadie,

Thanks for your reply. I have tried your method but still have some
problems. I move all the settings for gnuplot to the file named
``plot_all.gp.'' Then did the following coding in my script:

SettingFile=plot_all.gp


gnuplot -persist << EOF
$(cat $SettingFile)
EOF

The content of SettingsFile has been loaded into the shell script, but
all the variables (e.g., $terminal, $option, ...) were treated as
plain text without corresponding values loaded. Therefore, when I
execute the script, the gnuplot complained with error messages like:

gnuplot> set term $terminal enhanced $options
^
line 0: unknown or ambiguous terminal type; type just 'set
terminal' for a list

I have tried quotes around these variables, but they didn't help. How,
or is it possible, to make the variables written in ``plot_all.gp'' be
replaced by the assigned values?

hiankun

unread,
Apr 26, 2009, 9:55:41 PM4/26/09
to
If it's necessary, then I am sorry not to give the version information
about the bash I am working with:
GNU bash, version 3.2.39(1)-release (i486-pc-linux-gnu)

Jim

unread,
Apr 27, 2009, 9:06:32 AM4/27/09
to

Untried, but how about something like the following variations?
cat "$SettingFile" \
| sed \
-e "s/\$terminal/$terminal/g" \
-e "s/\$options/$options/g" \
| gnuplot -persist

or:
gnuplot -persist <<EOF
$(sed -e "s/\$terminal/$terminal/g" -e "s/\$options/$options/g" <
"$SettingsFile")
EOF

or (not sure about syntax):
gnuplot -persist <(sed -e "s/\$terminal/$terminal/g" -e "s/\$options/
$options/g" < "$SettingsFile")

Jon LaBadie

unread,
Apr 27, 2009, 5:20:23 PM4/27/09
to

Ugly, someone else will probably have a better solution, but try:

gnuplot -persist << EOF
$(bash -c 'x=$(cat settings) ; eval echo "\"$x\""')
EOF

hiankun

unread,
Apr 27, 2009, 11:38:55 PM4/27/09
to

Yes, your suggestion works! Thank you.

I used the 2nd variation of your suggestions and added every single
variable for the sed to substitute. Very straightforward approach, but
I didn't know it could be done by doing this.

hiankun

unread,
Apr 27, 2009, 11:59:16 PM4/27/09
to

Thanks for your approach, but I have little trouble when I was try to
understand it. (Actually, I don't understand the `echo' part.)
I am not sure whether I understand the meaning of your suggestion. The
``settings'' means my own settings filename, am I right? I rewrite it
as the follows.

$(bash -c 'x=$(cat $settingsFile); eval echo "\"$x\""')

And then after executing the shell script, it outputted no gnuplot's
result and seemed to trapped in the gnuplot part. (I have to Ctrl+C to
terminate the script.)

Jon LaBadie

unread,
Apr 28, 2009, 11:00:15 AM4/28/09
to

My bad. Yes I did mean the name of your settings file.
It could be hard coded or supplied as a variable.

There is a capitalization difference in your line above (settingsFile)
compared to your earlier script (SettingsFile). That could cause
cat to get no argument and sit there waiting for input. I considered
a safetynet of ${SettingsFile:?No Filename}. It should print an error
and exit if there is no value in the variable.

hiankun

unread,
Apr 29, 2009, 9:15:15 PM4/29/09
to

Sorry to confuse you. I forgot to mention that I've always used
``settingsFile'' as the variable's name in my script.

The capitalization couldn't be the cause, or I can't apply Jim's
method successfully. Therefore, maybe some parts else make the script
just sit there and output nothing?

hiankun

unread,
Apr 29, 2009, 10:38:28 PM4/29/09
to
Here are the script (plot_all.sh) and corresponding setting files
(plot_all.gp) and input data sample (input.plt):
http://docs.google.com/Doc?id=dhhksrx7_6djhpzcdk
http://docs.google.com/Doc?id=dhhksrx7_8dxbbnpd8
http://docs.google.com/Doc?id=dhhksrx7_7ddd8j2d4

Just for you who might want to test or need to learn how to combine
gnuplot with shell script as me.

All these files have been uploaded to Google Docs, which I have almost
no experience. If you cannot view the files or have problems in
testing, let me know please.

0 new messages