如何在Gnuplot中绘制具有加权边的树/图/网页? [英] How to plot a tree/graph/web with weighted edges in Gnuplot?
问题描述
我会针对您在问题中提到的答案提出一些细微的变化。假设顶点的坐标存储在文件 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脚本可能如下所示:
设置xr [0:50]
设置年[0:50]
设置尺寸平方
flePnts ='pnts.dat'
fleEdges ='edges.dat'
loadEdges = sprintf('< gawk''\
FNR == NR {x [$ 1] = $ 2; y [$ 1] = $ 3; next;} \
{printf%% f\t %% f\\\
%% 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\\\
,(x [$ 1] + x [$ 2])/ 2 + $ 4,(y [$ 1] + y [$ 2 ])/ 2 + $ 5,$ 3} \
''%s%s',flePnts,fleEdges);
plot \
使用1:2加载行的加载边线lc rgbblacklw 2 notitle,\
flePnts使用2:3:(0.6) lc rgbblacknotitle,\
flePnts使用带标签的2:3:1 tc rgbwhite字体Arial Boldnotitle,\
loadWeights使用带标签的1:2:3 tc rgbredcenter字体Arial Boldnotitle
-
loadEdges
命令调用gawk
,以便为所有边缘生成相应的x / y坐标对(用空行分隔) -
loadWeights
为每条边计算中间点并在这些坐标处放置一个标签(考虑到所需的偏移量)
最后,获得:
i am plotting a tree in gnuplot as discussed here (How to plot tree/graph/web data on gnuplot?). However, i would like to include the edges weight of the tree, i.e. for each edge i have a number (e.g. 10, 20, 30, 40) that represents the edge weight. The figure below shows in red the edges weight that i want to plot in gnuplot (i added this using power point).
Can anyone tell me how to plot edges with weight in gnuplot?
I would propose a slight variation on the answer which you mention in your question. Let's assume that the coordinates of the vertices are stored in a file pnts.dat
as follows:
0 5 10
1 20 20
2 15 15
3 30 30
4 40 10
Here, the first column records the corresponding label, while second and third columns contain the x- and y-coordinate, respectively.
The edges could be defined in a separate file edges.dat
as:
0 1 30 0 1
1 2 40 0 -2
1 4 20 0 1
1 3 10 0 1
Here, the first two column contain the point indices (they refer to the first column of pnts.dat
). The third column records the weight of a particular edge. Finally, the last two columns contain the x,y displacement of the generated associated label.
With this, the Gnuplot script could look like:
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
- the
loadEdges
command invokesgawk
in order to generate for all edges the corresponding pairs of x/y coordinates (delimited by a blank line) loadWeights
calculates for each edge the middle point and places a label at these coordinates (taking into account the required offset)
Finally, one obtains:
这篇关于如何在Gnuplot中绘制具有加权边的树/图/网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!