【问题标题】:Plotting arrows with gnuplot使用 gnuplot 绘制箭头
【发布时间】:2013-08-30 22:13:27
【问题描述】:

我有在模拟中生成的数据。生成的数据文件如下所示:

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 上)我更新了我的答案。

标签: gnuplot


【解决方案1】:

我认为没有一种自动方法可以根据第三列同时创建所有箭头。您必须为所需的每个箭头执行以下操作:

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:

【讨论】:

    【解决方案2】:

    如果您不想要箭头,那么您可以尝试脉冲样式(使用脉冲而不是线条) (如果您仍然想要顶部的线条,那么您可以绘制两次)。

    如果您真的想要箭头,那么以下方法可能会有所帮助:它使用 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
    

    (为了测试我去掉了时间格式你应该可以把它放回去没有太多麻烦。)

    然后我得到了这张图:

    我认为这或多或少是你想要的。

    【讨论】:

    • 做得很好,我总是从其他代码中调用 gnuplot,所以我没有意识到这样的脚本文件并不太复杂。但是,我确实认为,如果您自己生成输入文件,那么在代码中同时绘制箭头可能是最容易的。如果不是这个解决方案是完美的!
    • 感谢您的意见。但是,您的方法对我来说有点太复杂了(我不太了解 sed 和 awk ;)
    【解决方案3】:

    虽然这个问题很老了,但这是我的答案。

    可以使用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,则该点被视为未定义并被跳过。

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 2015-01-03
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多