我有一个由另一个 3rd 方应用程序创建的 .csv 数据文件,应该使用 gnuplot
绘制。 .
让我们假设文件具有以下格式:
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:
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"
问题是,这也绘制了第一行,因为它是数据。
我知道 to 可以通过以
#
开头来忽略数据文件中的任何行, 喜欢 #1;2;3;4;5;6
,但我不想编辑该文件,因为它也被其他工具使用。另一种方法是使用
plot <filename> every ::1
忽略第一行,这意味着我必须包含 every ::1
在上面的脚本中 5 次,如 in this link 所述.这将如下所示: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
对于每一个情节真的是唯一的方法吗?是否有一些更短的 - 更可取的单行 - 忽略第一(n)行的方法;一些定义 every ::1
的方法“全局”或类似(伪代码)set datafile ignorefirstnlines 1
?
最佳答案
Gnuplot 可以从第一行读取列名,但您仍然可以正常指定列名。因此,这有效地跳过了第一行。
发出命令
set key autotitle columnhead
这告诉 gnuplot 第一行不是数据,而是用于键的列名。您仍然可以
unset key
或 plot datafile title sometitle
和以前一样,gnuplot 不会使用这些数据。假设我的文件看起来像
1 2
4 5
7 8
我可以发出
set key autotitle columnhead
关注 unset key
(如果我真的不需要 key ),它将跳过第一行。![enter image description here](https://i.sstatic.net/uEDIP.png)
或者,我可以通过外部程序管道我的数据。例如,使用 awk(适用于包括 Windows 在内的大多数操作系统),我可以做到
plot "< awk '(NR>2){print;}' datafile"
跳过前 2 行(使用 Windows,我必须做
'< awk "(NR>2){print;}" datafile'
)。如果我不想继续输入这个,我可以将它存储在一个字符串中skipfile = "\"< awk '(NR>2){print;}' datafile\""
并将其用作宏(对于 Windows,使用
skipfile = '"< awk \"(NR>2){print;}\" datafile"'
)。例如,要使用线条绘制数据文件,我可能会这样做plot @skipfile with lines
@skipfile
只是告诉 gnuplot 处理命令就像我刚刚输入 skipfile
的内容一样在那里。
关于plot - Gnuplot:忽略数据文件中第一行的最短方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35526481/