11

I have 6 files which need to be plotted as line graphs with error margins and output them to different png files. The file format is as follows.

seconds mean-average min max

How would I go about plotting these graphs automatically? So I run a file called bash.sh and it will get the 6 files and output the graphs to different .png files. Titles and axis labels are also required.

13

If I understand correctly, this is what you want:

for FILE in *; do
    gnuplot <<- EOF
        set xlabel "Label"
        set ylabel "Label2"
        set title "Graph title"   
        set term png
        set output "${FILE}.png"
        plot "${FILE}" using 1:2:3:4 with errorbars
EOF
done

This assumes your files are all in the current directory. The above is a bash script that will generate your graphs. Personally, I usually write a gnuplot command file (call it, say, gnuplot_in), using a script of some form, with the above commands for each file and plot it using gnuplot < gnuplot_in.

To give you an example, in python:

#!/usr/bin/env python3
import glob
commands=open("gnuplot_in", 'w')
print("""set xlabel "Label"
set ylabel "Label2"
set term png""", file=commands)

for datafile in glob.iglob("Your_file_glob_pattern"):
    # Here, you can tweak the output png file name.
    print('set output "{output}.png"'.format( output=datafile ), file=commands )
    print('plot "{file_name}" using 1:2:3:4 with errorbars title "Graph title"'.format( file_name = datafile ), file=commands)

commands.close()

where Your_file_glob_pattern is something that describes the naming of your datafiles, be it * or *dat. Instead of the glob module, you can use os as well of course. Whatever generates a list of file names, really.

  • 1
    Your comment in your answer is a cleaner solution, why not expand the answer to show an example. +1 – bsd Mar 18 '12 at 13:32
  • Thanks for the comment. I was just doing that as you commented on the post. – Wojtek Rzepala Mar 18 '12 at 13:45
0

Bash solution, using a temporary command file:

echo > gnuplot.in 
for FILE in *; do
    echo "set xlabel \"Label\"" >> gnuplot.in
    echo "set ylabel \"Label2\"" >> gnuplot.in
    echo "set term png" >> gnuplot.in
    echo "set output \"${FILE}.png\" >> gnuplot.in
    echo "plot \"${FILE}\" using 1:2:3:4 with errorbars title \"Graph title\"" >> gnuplot.in
done
gnuplot gnuplot.in
0

This might help.

#set terminal postfile       (These commented lines would be used to )
#set output  "d1_plot.ps"    (generate a postscript file.            )
set title "Energy vs. Time for Sample Data"
set xlabel "Time"
set ylabel "Energy"
plot "d1.dat" with lines
pause -1 "Hit any key to continue"

Execute the script file as gnuplot filename.

Click here for more details.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.