Hi all

Please help me out.

I plot a file output1.txt in the following way ::

gnuplot> plot "output1.txt" using 1:2 with lines, \
plot "output1.txt" using 1:3 with lines

BUT now I have many data files,

output1.txt, output2.txt, output3.txt, ....,....... ,output1000.txt

I need to plot each one of them in gnuplot, and save them respectively in

graph1.jpeg, graph2.jpeg, graph3.jpeg, ............, graph1000.jpeg

It is very cumbersome if I plot and save 1 file at a time.

Is there an easy way, using loops in a Shell script that can PLOT and SAVE them

Please kindly help
Regards
Alice

Recommended Answers

All 2 Replies

I'm assuming that all of plots are identical.

First you need to set up a template file containg the command for gnuplot like this.

# template.gnuplot
set term png
set output "OUTFILE"
plot "DATAFILE" using 1:2 with lines \
     "DATAFILE" using 1:3 with lines

Now you can plot the datafiles. This script will loop from 1 to 10, modifying the gnuplot command file on each iteration.

# Plotting.sh
#!/bin/bash

tmpfile=/tmp/temp.$$
touch $tmpfile

x=1
while [ $x -le 10 ]
do
  sed '
        s/DATAFILE/output'${x}'.txt/
        s/OUTFILE/graph'${x}'.png/' template.gnuplot # > $tmpfile
  gnuplot $tmpfile
  x=$((x + 1))
done
rm /tmp/temp.$$

Thanks dear shibblez.
Your code was helpful.

I have also figured out an another way as told to me by a friend. It is

i = 1
n = 1000
set terminal jpeg
load "loop.plt"

"loop.plt" contains:

filename = "output".i.".txt"
plotfile = "graph".i.".jpg"
print filename." ".plotfile

set output plotfile
plot filename using 1:2, "" using 1:3
set output

i=i+1

if (i <= n) reread

Regards
Alice

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.