【问题标题】:How to plot a tree/graph/web with weighted edges in Gnuplot?如何在 Gnuplot 中绘制带有加权边缘的树/图/网络?
【发布时间】:2017-02-24 20:26:58
【问题描述】:

我正在 gnuplot 中绘制一棵树,正如这里所讨论的 (How to plot tree/graph/web data on gnuplot?)。但是,我想包括树的边权重,即对于每条边,我都有一个代表边权重的数字(例如 10、20、30、40)。下图以红色显示了我想在 gnuplot 中绘制的边缘权重(我使用 power point 添加了这个)。

谁能告诉我如何在 gnuplot 中用权重绘制边缘?

【问题讨论】:

    标签: graph tree gnuplot


    【解决方案1】:

    我会建议您在问题中提到的答案略有不同。假设顶点的坐标存储在文件pnts.dat中,如下所示:

    0   5   10
    1   20  20
    2   15  15
    3   30  30
    4   40  10
    

    这里,第一列记录对应的标签,第二列和第三列分别包含x坐标和y坐标。

    边缘可以在单独的文件edges.dat 中定义为:

    0   1   30  0   1
    1   2   40  0   -2
    1   4   20  0   1
    1   3   10  0   1
    

    这里,前两列包含点索引(它们引用pnts.dat 的第一列)。第三列记录特定边的权重。最后,最后两列包含生成的关联标签的 x,y 位移。

    这样,Gnuplot 脚本可能如下所示:

    set xr [0:50]   
    set yr [0:50]
    
    set size square   
    
    flePnts = 'pnts.dat'
    fleEdges = 'edges.dat'
    
    loadEdges = sprintf('< gawk '' \
        FNR==NR{x[$1]=$2;y[$1]=$3;next;} \
        {printf "%%f\t%%f\n%%f\t%%f\n\n", x[$1], y[$1], x[$2], y[$2];} \
    '' %s %s', flePnts, fleEdges); 
    
    loadWeights = sprintf('< gawk '' \
        FNR==NR{x[$1]=$2;y[$1]=$3;next;} \
        {printf "%%f\t%%f\t%%s\n", (x[$1]+x[$2])/2 + $4, (y[$1]+y[$2])/2 + $5, $3} \
    '' %s %s', flePnts, fleEdges);
    
    plot \
        loadEdges using 1:2 with lines lc rgb "black" lw 2 notitle, \
        flePnts using 2:3:(0.6) with circles fill solid lc rgb "black" notitle, \
        flePnts using 2:3:1 with labels tc rgb "white" font "Arial Bold" notitle, \
        loadWeights using 1:2:3 with labels tc rgb "red" center font "Arial Bold" notitle
    
    1. loadEdges 命令调用 gawk 以便为所有边生成相应的 x/y 坐标对(由空行分隔)
    2. loadWeights 为每条边计算中间点并在这些坐标处放置一个标签(考虑到所需的偏移量)

    最后,得到:

    【讨论】:

      【解决方案2】:

      另外,如果要在边缘添加箭头,则必须执行以下步骤:

      1. 添加箭头样式:

        set style arrow 1 head filled size screen 0.025,10,40 lc rgb "black" lw 2
        
      2. with lines命令更改为withvector

        loadEdges using 1:2:3:4 with vectors arrowstyle 1  notitle, \
        

      下图展示了最终结果:

      这是最终的代码:

          set xr [0:50]
          set yr [0:50]
      
          set size square
      
          set style arrow 1 head filled size screen 0.025,10,40 lc rgb "black" lw 2
      
          flePnts = 'pnts.dat'
          fleEdges = 'edges.dat'
      
          loadEdges = sprintf('< gawk '' \
              FNR==NR{x[$1]=$2;y[$1]=$3;next;} \
              {printf "%%f\t%%f\t%%f\t%%f\n\n", x[$1], y[$1], (x[$2]-x[$1]), (y[$2]-y[$1]);} \
          '' %s %s', flePnts, fleEdges);
      
          loadWeights = sprintf('< gawk '' \
              FNR==NR{x[$1]=$2;y[$1]=$3;next;} \
              {printf "%%f\t%%f\t%%s\n", (x[$1]+x[$2])/2 + $4, (y[$1]+y[$2])/2 + $5, $3} \
          '' %s %s', flePnts, fleEdges);
      
          plot \
              loadEdges using 1:2:3:4 with vectors arrowstyle 1  notitle, \
              flePnts using 2:3:(0.6) with circles fill solid lc rgb "black" notitle, \
              flePnts using 2:3:1 with labels tc rgb "white" font "Arial Bold" notitle, \
              loadWeights using 1:2:3 with labels tc rgb "red" center font "Arial Bold" notitle
      

      【讨论】:

        猜你喜欢
        • 2020-12-16
        • 2013-12-22
        • 1970-01-01
        • 2018-01-05
        • 2016-02-16
        • 1970-01-01
        • 1970-01-01
        • 2018-12-01
        • 1970-01-01
        相关资源
        最近更新 更多