Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

1 Answer

up vote 6 down vote accepted

If I understand correctly, this is what you want:

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

share|improve this answer
Your comment in your answer is a cleaner solution, why not expand the answer to show an example. +1 – bdowning 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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