Derrick
Derrick

Reputation: 2366

How do I plot a collection of csv files in gnuplot?

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?

Upvotes: 1

Views: 8822

Answers (3)

mgilson
mgilson

Reputation: 310307

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

Upvotes: 7

andyras
andyras

Reputation: 15930

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.

Upvotes: 2

Dan
Dan

Reputation: 12705

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 

Upvotes: 2

Related Questions