Ran*_*mao 2 pivot-table gnuplot
我想使用 Gnuplot 绘制一种数据透视图。所以我需要忽略文件中的一些数据行。我尝试了以下方法:
unset key
set xtics font "Times-Roman, 5"
set ytics font "Times-Roman, 5"
set multiplot layout 4,3 #title "Multiplots\n"
plot [7:50][0:1] 'forStackoverGnuplot.txt' using 1:($2==0?($3==4?$4:NaN):NaN) with lines ti '4'
plot [7:50][0:1] 'forStackoverGnuplot.txt' using 1:($2==0?($3==4?$4:"fe"):"fe") with lines ti '4'
Run Code Online (Sandbox Code Playgroud)
数据文件:
20 0 5 0.668593155
7 0 4 0.885223087
20 0 5 0.668593155
10 0 4 0.92239289
20 0 5 0.668593155
20 0 4 0.834947746
30 0 4 0.693726036
50 0 4 0.47169919
Run Code Online (Sandbox Code Playgroud)
但我得到:

这不是我预期的结果。我能做什么?我想让数据线交错。
基本上,gnuplot区分丢失和无效的数据点,参见例如在 gnuplot 中,“设置数据文件丢失”,如何同时忽略“nan”和“-nan”?.
如果您有一个未定义的点(例如 asNaN或1/0),则绘图线会中断。要实现这一点,您需要设置datafile missing. 但是,如果您对using语句中的某些内容进行评估,那将不起作用,因为那么对于“未定义”<->“缺失”选择(例如选择列using 1:4)来说就太晚了。所以声明
set datafile missing '?'
plot 'forStackoverGnuplot.txt' using 1:(($2==0 && $3==4) ? $4 : '?')
Run Code Online (Sandbox Code Playgroud)
也没有工作。
相反,您必须在外部过滤数据,并在绘制它们之前删除受影响的行:
unset key
set style data linespoints
set multiplot layout 1,2
plot [7:50][0:1] 'forStackoverGnuplot.txt' using 1:(($2==0 && $3==4) ? $4 : 1/0)
filter = '< awk ''{ if ($2 == 0 && $3 == 4) print $1, $2, $3, $4}'' '
plot [7:50][0:1] filter.' forStackoverGnuplot.txt' using 1:4
unset multiplot
Run Code Online (Sandbox Code Playgroud)
这给出:

在左图中,绘制了点,但未通过线连接,因为它们之间存在“无效”点。