We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,831 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to plot and save many data files in GNUPLOT, inside a loop in a SHELL SCRIPT

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

2
Contributors
2
Replies
17 Hours
Discussion Span
2 Years Ago
Last Updated
6
Views
alice06
Newbie Poster
6 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.$$
shibblez
Junior Poster in Training
73 posts since Oct 2010
Reputation Points: 15
Solved Threads: 6
Skill Endorsements: 0

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

alice06
Newbie Poster
6 posts since Oct 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0802 seconds using 2.63MB