Gnuplot:来自数据的围栏图

时间:2013-04-15 15:02:34

标签: gnuplot fence-plots

我正在试图弄清楚如何在gnuplot中绘制围栏图,但我很难理解我在互联网上找到的示例中发生了什么。

我在模拟中的不同时间点有一个(变化的)数据集,数据文件组织为值 1 的矩阵:

t1    x11    y11   // indices here indicate that (x1,y1) are a data point which
t1    x21    y21   // I'd plot on a regular 2D plot for this timestep, with the
...                // additional index noting which time step the values are for.
t1    xN1    yN1   
[blank line]
t2    x12    y12
t2    x22    y22
...
t2    xN2    yN2
[etc...]
tM    xNM    yNM

我想用每个时间值的一个围栏来绘制它。我可以简单地绘制splot 'data.txt'并绘制与我想要的东西非常相似的东西 - 沿着围栏“顶边”的+标记,x轴上的时间,y轴上的x数据和z轴上的y数据。但是,如果我在w lines命令中添加splot之类的内容,我只会得到一个连接了所有数据系列的表面。

我试图调整the demo script collection中的示例(大约一半),但它们都依赖于虚拟变量,我无法弄清楚如何将其与我的数据系列相结合。我也找到了一些其他的例子,但它们都非常复杂,我不明白它们的用途。

使用gnuplot从数据创建栅栏图的好方法是什么?


1 如果有必要,可以改变这一点 - 我控制生成数据的代码。但这很麻烦......

1 个答案:

答案 0 :(得分:3)

遗憾的是,这确实需要对数据进行一些更改。这个变化非常小,可能只需要一个简单的awk 1,2 脚本来处理:

这是我的交互式gnuplot会话的复制/粘贴:

gnuplot> !cat test.dat
1 2 3
1 2 0

1 3 4
1 3 0

1 4 5
1 4 0


2 2 3
2 2 0

2 3 4
2 3 0

2 4 5
2 4 0


3 2 3
3 2 0

3 3 4
3 3 0

3 4 5
3 4 0
!
gnuplot> splot 'test.dat' u 1:2:3 w lines

这里需要注意的是,“围栏”之间有2个空白行,每个x,y数据点后面出现两次,后面是一个空白行。第二次出现时,z坐标为0。

让每个围栏都有不同的颜色:

gnuplot> splot for [i=0:3] 'test.dat' index i u 1:2:3 w lines

awk脚本甚至可以内联完成:

splot "< awk {...} datafile"

但引用可能会有点棘手(在单引号字符串中包含单引号,你加倍)......

AWKCMD='awk ''{if(!NF){print ""}else if(index($0,"#")!=1){printf "%s %s %s\n%s %s 0\n\n", $1,$2,$3,$1,$2}}'' '
splot '<'.AWKCMD.'datafile.dat' u 1:2:3 w lines

就效率而言,我相信我上面使用的迭代将在每次迭代时调用awk命令。这里的解决方法是从索引号中提取颜色:

splot '<'.AWKCMD.' test.dat' u 1:2:3:(column(-2)) w l lc variable

我相信这只会根据需要执行一次awk命令,因此只有一百万个条目它仍然应该相对较快地响应。


1 awk '{if(!NF){print ""}else{printf "%s %s %s\n%s %s 0\n\n", $1,$2,$3,$1,$2}}' test.dat

2 awk '{if(!NF){print ""}else if(index($0,"#")!=1){printf "%s %s %s\n%s %s 0\n\n", $1,$2,$3,$1,$2}}' test.dat(忽略评论的版本)