使用gnuplot绘制文件中的所有列,而不指定列数 [英] Plot all columns in a file using gnuplot without specifying number of columns

查看:177
本文介绍了使用gnuplot绘制文件中的所有列,而不指定列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量要使用gnuplot绘制的数据文件。文件为文本形式,为多列形式。我想使用gnuplot绘制给定文件中的所有列,而无需确定要绘制的列数,甚至不需要确定文件中的列总数,因为列总数在我拥有的文件。有什么办法可以使用gnuplot做到这一点吗?

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?

推荐答案

您可以通过多种方式来实现此目的,有些则越来越不优雅。

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

以以下文件 data 为例:

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

这有3列,但是您想编写一个通用脚本而不假定任何特定数字。我要解决的方法是使用 awk 通过 system()<在gnuplot脚本中获取文件中的列数。 / code>呼叫:

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

假设您不想使用 system()调用,并且知道列数将始终低于某个最大值,例如10:

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

然后gnuplot会抱怨不存在数据,但仍会绘制第1至3列。

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

这篇关于使用gnuplot绘制文件中的所有列,而不指定列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆