Auto plotting all columns

You can count the number of columns in your file using awk and then do a looped plot. There might be a function to get the number of columns in your data file already implemented in gnuplot but I do not know it. You can try this:

N=`awk 'NR==1 {print NF}' Data.txt`
plot for [i=2:N] "Data.txt" u 1:i

If your first row contains a comment (starting by #) change NR== to the appropriate value. If you have a variable number of columns for different rows then you might want to complicate the awk command.


Seeing that this questions is very old, I still think it is worth revisiting, as you now (Version 5.2) have access to the number of columns in a file without relying on external tools.

DATA = 'path/to/datafile.txt'
stats DATA

will (among other stuff) store the number of columns in the variable STATS_columns, so now you can do something like:

N=STATS_columns
plot for [i=2:N] DATA using 1:i title DATA.' '.i with lines

which will plot all the columns (assuming the first column is used for the x-axis) with legend entries matching the filename plus the column number.

PS: Not sure when this feature was introduced, but it's there now. :)


@Paul shows a correct answer, but an even simpler variant is possible. You can use an open-ended iteration that stops when it runs out of columns:

plot for [n=1:*] "data.dat" using 1:n title sprintf("Column %d",n)

Tags:

Gnuplot