Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot all columns in a file using gnuplot without specifying number of columns

I have large number of files of data which I want to plot using gnuplot. The files are in text form, in the form of multiple columns. I wanted to use gnuplot to plot all columns in a given file, without the need for having to identify the number of the columns to be plotted or even then total number of columns in the file, since the total number of columns tend to vary between the files I am having. Is there some way I could do this using gnuplot?

like image 920
Ajoy Avatar asked Dec 24 '22 21:12

Ajoy


1 Answers

There are different ways you can go about this, some more and some less elegant.

Take the following file data as an example:

1 2 3
2 4 5
3 1 3
4 5 2
5 9 5
6 4 2

This has 3 columns, but you want to write a general script without the assumption of any particular number. The way I would go about it would be to use awk to get the number of columns in your file within the gnuplot script by a system() call:

N = system("awk 'NR==1{print NF}' data")
plot for [i=1:N] "data" u 0:i w l title "Column ".i

enter image description here

Say that you don't want to use a system() call and know that the number of columns will always be below a certain maximum, for instance 10:

plot for [i=1:10] "data" u 0:i w l title "Column ".i

Then gnuplot will complain about non-existent data but will plot columns 1 to 3 nonetheless.

like image 69
Miguel Avatar answered May 19 '23 03:05

Miguel