2011-01-27 163 views
6

我在模拟中生成了数据。所生成的数据文件看起来是这样的:用gnuplot绘制箭头

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创建第一个情节的箭头帮助?

+0

嗨,我想我得到了你的gnuplot问题工作(至少在linux上)我更新了我的答案。 – Tom 2011-01-27 15:40:37

回答

3

我不认为有一种自动方式可以基于第三列同时创建所有箭头。你将不得不执行对您希望每个箭头下面:

set arrow xval1,yval1 to xval2,yval2 

您也可以使用相对箭头

set arrow xval1,yval1 rto 1,0 

这将吸引来自xval1横向箭头,yval1到(xval1 + 1) ,yval1

plenty of options associated with the set arrow command

3

如果你不想箭头头,那么你可以尝试的冲动风格(与冲动,而不是用线) (如果你仍然想在最上面的线,那么你可以绘制两次)。

如果您确实需要箭头,那么以下几点可能会有所帮助:它使用for循环(或排序)将垂直箭头添加到绘图。

Gnuplot script, for loop within or adding to existing plot

具体做法是:

创建一个文件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

我认为这或多或少你想要什么。

+0

做得很好,我总是从其他代码中调用gnuplot,所以我没有意识到这样的脚本文件不是太复杂。不过,我认为如果您自己生成输入文件,可能最容易在代码中同时绘制箭头。如果不是这个解决方案是完美 – Martin 2011-01-27 16:46:42

1

虽然这个问题已经很陈旧了,但这是我的答案。

人们可以使用vectors绘图风格,可以使用基于列的值变量arrowstyles:

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,该点被认为是不确定的和被跳过。