1
votes

I'm trying to plot the 1st and 3rd columns of multiple files, where each file is supposed to be plotted to an own output.png. My files have the following names:

VIB2--135--398.6241
VIB2--136--408.3192
VIB2--137--411.3725
...

The first number in the file name is an integer, which ranges from 135-162. The second number is just a decimal number and there is no regular spacing between the values. Basically I want to do something like this

plot for [a=135:162] 'VIB2--'.a.'--*' u 1:3 w l

although this doesn't work, of course, since the ' * ' is just the placeholder I know from bash and I don't know, if there is something similar in gnuplot. Furthermore, each of the files should be, as already said above, plotted to its own output.png, where the two numbers should be in the output name, e.g. VIB2--135--398.6241.png.

I tried to come up with a bash script for this, like (edited):

#!/bin/bash

for file in *
do

gnuplot < $file

set xtics 1
set xtics rotate
set terminal png size 1920,1080 enhanced
set output $file.png
plot "$file" u 1:3 w l 

done but I still get

gnuplot> 1    14   -0.05
         ^
         line 0: invalid command
gnuplot> 2    14    0.01
         ^
         line 0: invalid command
...

which are actually the numbers from my input file. So gnuplot thinks, that the numbers I want to plot are commands... ?? Also, when the end of the file is reached, I get the following error message

#PLOT 1
plot: an unrecognized command `0x20' was encountered in the input
plot: the input file `VIB2--162--496.0271' could not be parsed

I've seen a few questions similar to mine, but the solutions didn't really work for me and I cannot add a comment, since I do not have the reputation. Please help me with this.

1
Seems to me that you used the final '*' as a regular expression. I dont know if gnuplot accept this (maybe the syntax is different). Anw, I do what you want with a python script.kist
@kist: and could you send me the python script?drunk user
I got the same message, when I attempted to run plot, without first calling gnuplot.j0h

1 Answers

1
votes

gnuplot < $file starts gnuplot and feeds it the content of $file as input. That means gnuplot will now try to execute the commands in the data file which doesn't work.

What you want is a "here document":

gnuplot <<EOF
set xtics 1
set xtics rotate
set terminal png size 1920,1080 enhanced
set output $file.png
plot "$file" u 1:3 w l 
EOF

What this does is: The shell reads the text up to the line with solemn EOF, replaces all variables, puts that into a temporary file and then starts gnuplot feeding it the temporary file as input.

Be careful that the file names don't contain spaces, or set output $file.png will not work. To be safe, you should probably use set output "$file.png" but my gnuplot is a bit rusty.