Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto plotting all columns

Tags:

gnuplot

I have a file with several columns of data (the number of columns N might me quite large). I want to plot all the columns as a function of the first one (that is, plot 'Data.txt' using 1:2, 'Data.txt' using 1:3, ..., 'Data.txt' using 1:N). The thing is, I want this command to work when I don't know the number of columns. Is that possible?

like image 811
Valentine Avatar asked Jul 03 '14 20:07

Valentine


3 Answers

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.

like image 159
Miguel Avatar answered Nov 28 '22 08:11

Miguel


@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)
like image 22
Ethan Avatar answered Nov 28 '22 08:11

Ethan


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. :)

like image 44
Paul Avatar answered Nov 28 '22 09:11

Paul