1

I have a collection of csv files with the same 2 column format. I'd like to produce separate xy scatter plots corresponding to each file, but with the same style. The only thing that should change is the input and output filenames. How to do it?

3 Answers 3

7

The solution posted by andyras is perfectly workable. However, in these instances, "HERE" files are typically better since it avoids spawning an extra process and since you won't have problems with mixing single quotes and double quotes ...

for file in $(echo *.dat); do
    gnuplot <<EOF
    set terminal post enh
    set output "output_${file}.ps"
    set datafile separator ','  #csv file
    plot "$file" u 1:2
EOF
done
Sign up to request clarification or add additional context in comments.

1 Comment

I like this one because it is cleaner. Thanks!
2

First, create a text file containing all of the style information, say gplot_prefix.txt. Then, I assume you have some pattern that matches all of the files you want to plot, say *.dat. Then, make a zsh script as follows:

foreach arg in $@
    filename=${arg}_plotfile.pl
    cp gplot_prefix.txt ${filename}
    echo set output ${arg}.png >>${filename}
    echo plot \"${arg}\" u 1:2 >>${filename}
    gnuplot ${filename}
    rm ${filename}

(this may have bugs; my zsh isn't working correctly right now) and call it like

./plotscript.zsh *.dat 

Comments

2

You can create a wrapper bash script and save it as plot.sh:

#!/bin/bash

echo "set terminal postscript enhanced
set output 'output_$1.eps'

plot '$1'

Let's say your data files all have the .dat extension. You would use this by calling

for datfile in $(ls *dat) ; do ./plot.sh $datfile ; done

at the command line in bash.

1 Comment

Oh, @Dan posted pretty much the same solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.