Gnuplot:忽略数据文件第一行的最快捷方法 [英] Gnuplot: Shortest way to ignore first line in datafile

查看:274
本文介绍了Gnuplot:忽略数据文件第一行的最快捷方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由另一个第三方应用程序创建的.csv数据文件,应使用gnuplot进行绘制. 假设文件具有以下格式:

I have a .csv datafile created by another 3rd party application that should be plotted using gnuplot. Let's assume the file has the following format:

1;2;3;4;5;6 <-- This is the header line that should be ignored (with values 1;2;...;N)
1;1;2;1;1;1
2;3;3;3;5;6
3;4;1;1;1;4

第一列是x轴,随后的每一列都应绘制为自己的线图(是的,我知道,一个图中的太多线图可​​能看起来很糟,但这只是一个主意).这是MCVE:

The first column is the x-axis, the following columns should each be plotted as own lineplot (yes, I know, too many line plots within one plot may look bad, but just to get an idea). Here a MCVE:

set terminal png size 1000,500    
set datafile separator ";" # CSV file is seperated with ;
plot \
  'C://tmp/test.csv' using 1:2 with lines title "A",\
  'C://tmp/test.csv' using 1:3 with lines title "B",\
  'C://tmp/test.csv' using 1:4 with lines title "C",\
  'C://tmp/test.csv' using 1:5 with lines title "D",\
  'C://tmp/test.csv' using 1:6 with lines title "E"

问题是,这还会绘制第一行,因为它可能是数据.

The problem is, that this also plots the first line as it would be data.

我知道可以像#1;2;3;4;5;6那样通过以#开头来忽略数据文件中的任何行,但是我不想编辑该文件,因为它也被其他工具使用.

I know that to can ignore any line in the datafile by starting it with #, like #1;2;3;4;5;6, yet I do not want to edit the file, because it is also used by other tools.

另一种方法是使用plot <filename> every ::1忽略第一行,这意味着我必须在上述脚本中将every ::1包含5次,如

Another way is to use plot <filename> every ::1 to ignore the first line, which would mean that I would have to include every ::1 5 times in the above script, as explained in this link. This would look like the following:

set terminal png size 1000,500    
set datafile separator ";" # CSV file is seperated with ;
plot \
  'C://tmp/test.csv' every ::1 using 1:2 with lines title "A",\
  'C://tmp/test.csv' every ::1 using 1:3 with lines title "B",\
  'C://tmp/test.csv' every ::1 using 1:4 with lines title "C",\
  'C://tmp/test.csv' every ::1 using 1:5 with lines title "D",\
  'C://tmp/test.csv' every ::1 using 1:6 with lines title "E"

为每个图定义every ::1确实是唯一的方法吗?是否有一些更短的-最好是单线-忽略第一行的方法;以某种方式全局"定义every ::1还是类似(伪代码)set datafile ignorefirstnlines 1?

Is defining every ::1 for every plot really the only way? Is there some shorter - preferable one-liner - way to ignore the first (n) line(s); some way to define the every ::1 "globally" or something like (pseudocode) set datafile ignorefirstnlines 1?

推荐答案

Gnuplot可以从第一行读取列名,但是您仍然可以正常指定列名.因此,这有效地跳过了第一行.

Gnuplot can read column names from the first line, but you can still specify the column names normally as well. Therefore, this effectively skips the first line.

发出命令

set key autotitle columnhead

这告诉gnuplot第一行不是数据,而是用于键的列名.您仍然可以像以前一样unset keyplot datafile title sometitle,并且gnuplot不会使用该数据.

This tells gnuplot that the first line is not data, but is the column names to be used for the key. You can still unset key or plot datafile title sometitle the same as you could before, and gnuplot will just not use that data.

假设我的文件看起来像

1 2
4 5
7 8

我可以按照unset key的要求发布set key autotitle columnhead(如果我真的不想要键),它将跳过第一行.

I can just issue set key autotitle columnhead follwed by unset key (if I don't really want a key), and it will skip the first line.

或者,我可以通过外部程序通过管道传输数据.例如,使用awk(适用于包括Windows在内的大多数操作系统),我可以

Alternatively, I can pipe my data through an external program. For example, using awk (which is available for most OS's including Windows), I can do

plot "< awk '(NR>2){print;}' datafile"

跳过前两行(使用Windows,我必须执行'< awk "(NR>2){print;}" datafile').如果我不想继续输入,可以将其存储在字符串中

to skip the first 2 lines (using Windows, I must do '< awk "(NR>2){print;}" datafile'). If I don't want to keep typing this, I can store this in a string

skipfile = "\"< awk '(NR>2){print;}' datafile\""

并将其用作宏(对于Windows,请使用skipfile = '"< awk \"(NR>2){print;}\" datafile"').例如,要使用线条绘制数据文件,我可能会这样做

and use it as a macro (for Windows, use skipfile = '"< awk \"(NR>2){print;}\" datafile"'). For instance, to plot the datafile using lines, I might do

plot @skipfile with lines

@skipfile只是告诉gnuplot对待命令,就像我刚才在此处键入skipfile的内容一样.

The @skipfile just tells gnuplot to treat the command like I had just typed the contents of skipfile right there.

这篇关于Gnuplot:忽略数据文件第一行的最快捷方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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