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

我只是一个虾纸丫 提交于 2019-12-07 07:03:22

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/29078933/plot-all-columns-in-a-file-using-gnuplot-without-specifying-number-of-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!