使用gnuplot绘制箭头

7

我有一些模拟生成的数据。生成的数据文件看起来像这样:

1990/01/01 99
1990/01/02 92.7
1990/01/03 100.3
1990/01/04 44.2
1990/01/05 71.23
...
2100/01/01 98.25

我可以通过简单地输入(冗长的)命令来创建图表:

plot "simulation.dat" using 1:2 with line

我想添加第三列,用于添加箭头信息。第三列的编码如下:

  • 0 => 对于该x轴值不绘制箭头
  • 1 => 绘制指向上方的箭头,用于该x轴值
  • 2 => 绘制指向下方的箭头,用于该x轴值

我刚开始学习gnuplot,需要帮助来了解如何使用gnuplot在第一个图形上创建箭头?


嗨,我想我已经解决了你的gnuplot问题(至少在Linux上)。我更新了我的答案。 - Tom
3个回答

4

我认为没有一种自动化的方式可以根据第三列同时创建所有箭头。你需要对每个想要的箭头执行以下操作:

set arrow xval1,yval1 to xval2,yval2

你也可以使用相对箭头

set arrow xval1,yval1 rto 1,0

这将从xval1,yval1绘制一条水平箭头到(xval1+1),yval1。
使用set arrow命令有许多选项相关联

3
如果您不想要箭头,那么可以尝试脉冲样式(使用脉冲而不是线条)(如果您仍然想要顶部的线条,则可以绘制两次)。
如果您真的想要箭头,则以下内容可能会有所帮助:它使用for循环(或排序)向绘图添加垂直箭头。 Gnuplot脚本,在现有绘图中添加for循环 具体来说:
创建一个名为simloop.gp的文件,其内容如下:
count  = count+1
#save the count to count.gp
system 'echo '.count.' > count.gp'
#load the simloop shell
system "./simloop.sh"

#draw the arrow
load 'draw_arrow.gp'

if(count<max) reread

然后创建一个类似于以下内容的 simloop.sh 文件
#!/bin/bash

#read the count
count=$(awk -F, '{print $1}' count.gp)
#read the file
xcoord=$(awk -v count=$count -F, 'BEGIN{FS=" ";}{ if(NR==count) print $1}' simulation.dat)
ycoord=$(awk -v count=$count -F, 'BEGIN{FS=" "}{ if(NR==count) print $2}' simulation.dat)
dir=$(awk -v count=$count -F, 'BEGIN{FS=" "}{ if(NR==count) print $3}' simulation.dat)

#choose the direction of the arrow
if [ \"$dir\" == \"0\" ]; then
    echo '' > draw_arrow.gp
fi

if [ \"$dir\" == \"1\" ]; then
  echo 'set arrow from ' $xcoord' ,0 to '$xcoord','$ycoord' head' > draw_arrow.gp
fi

if [ \"$dir\" == \"2\" ]; then
 echo 'set arrow from '$xcoord',0 to '$xcoord','$ycoord' backhead' > draw_arrow.gp
fi

然后创建一个类似如下的simulation.gp文件:
count = 0;
max = 5;
load "simloop.gp"
set yrange[0:*]
plot "simulation.dat" u 1:2 w l

请确保shell文件具有可执行权限(chmod +wrx simloop.sh),加载gnuplot并输入以下命令:

load "./simulation.gp"

这对我来说很有效,使用了数据文件

1  99   0
2  92.7 1
3 100.3 2
4 44.2  0
5 71.23 1

(为了测试,我去掉了时间格式。您应该可以很容易地将其放回去。)
然后我得到了这张图表:enter image description here 我认为这差不多是您想要的。

做得不错!我一直在其他代码中调用gnuplot,所以没有意识到这样的脚本文件并不复杂。然而,我认为如果你自己生成输入文件,在代码中同时绘制箭头可能会更容易。如果不行,这个解决方案也是完美的! - Martin
感谢您的输入。不过,您的方法对我来说有点太复杂了(我对sed和awk不是很熟悉;) - oompahloompah

2
尽管这个问题很老,但是这是我的答案。
可以使用 "vectors" 绘图样式,它可以根据列的值使用可变箭头样式:
set style arrow 1 backhead
set style arrow 2 head
set yrange[0:*]
set xdata time
set timefmt "%Y/%m/%d"
plot "simulation.dat" using 1:2 with line,\
     "" using 1:2:(0):(-$2):($3 == 0 ? 1/0 : $3) with vectors arrowstyle variable

如果一列的值为1/0,则该点被视为未定义并将被跳过。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接