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?
Plot all columns in a file using gnuplot without specifying number of columns
Asked Answered
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
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.
@Olecranon
STATS_columns
is available since 5.0 –
Tidings @Tidings Thanks for the clarification, actually running
stats
was the first thing I did before posting the answer, and I didn't see anything pertaining to column number (I run 4.6.4). –
Lezlie I thought so. After Thor's comment I also had to look when this was introduced. Gnuplot 5 can also extract matrix dimensions from a data file. –
Tidings
Now you can use "*" symbol:
plot for [i=1:*] 'data' using 0:i with lines title 'Column '.i
yes, as far as I can tell it is documented from gnuplot 5.0.3 on (Feb 2016). Check
help for loops
. –
Illuminometer © 2022 - 2024 — McMap. All rights reserved.
stats
command, so if you runstats 'data' nooutput
theSTATS_columns
variable will contain the number of columns, 3 in this case. – Olecranon