如何使用权重标记图边

Dr.*_*ius 7 wolfram-mathematica mathematica-8

警告!当Mathematica v 8.0是最酷的孩子时,我发布了这个问题.从版本9.0.1开始,该错误已得到解决

The help for EdgeLabels 状态:

在此输入图像描述

然而:

CompleteGraph[4,
 EdgeWeight -> Range@6,
 VertexShapeFunction -> "Name",
 EdgeLabels -> "EdgeWeight"]
Run Code Online (Sandbox Code Playgroud)

结果是:

在此输入图像描述

所以,没有Edge Labels ......我想这是一个bug.

我使用了一个讨厌的结构,如:

adj = {{\[Infinity], 1, 1, 1, 1}, {1, \[Infinity], 2, 2, 2}, 
       {1, 2, \[Infinity], 2, 2}, {1, 2, 2, \[Infinity], 2}, 
       {1, 2, 2, 2, \[Infinity]}};

WeightedAdjacencyGraph[adj,
    VertexShapeFunction -> "Name", 
    EdgeLabels -> 
     MapThread[Rule,{EdgeList@#,AbsoluteOptions[#, EdgeWeight]/.{_ -> x_}-> x}], 
    GraphHighlight -> FindEdgeCover[#]]  
                                        &@ WeightedAdjacencyGraph[adj]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

好主意?

Ver*_*eia 9

对于常规GraphPlot,您需要使用EdgeRenderingFunction(文档)稍微复杂的解决方案.假设您有一个邻接矩阵,其中元素也是(方向)权重.

lilnums = {{0, 2., 1., 3., 0, 6.}, {0, 0, 1., 2., 0, 0}, {1., 8., 0, 2., 0, 
 2.}, {10., 13., 7., 0, 0, 10.}, {0, 0, 0, 0, 0, 0}, {4., 1., 1., 2.,
 2., 0}}
Run Code Online (Sandbox Code Playgroud)

以下是顶点的一些标签,假设您正在绘制国际银行间风险暴露的网络图(原始国家有更多国家!).

names = {"AT", "AU", "CA", "CH", "CL", "ES"}
Run Code Online (Sandbox Code Playgroud)

以下是您需要的.这些技巧是使用零件#2规范内部的部分返回到邻接矩阵的参考,引用正确的元素nums,并Mean[#1]在边缘的中点定位标签.插槽#1似乎保持顶点的坐标.

GraphPlot[lilnums, DirectedEdges -> True, 
 VertexRenderingFunction -> ({White, EdgeForm[Black], Disk[#, .04], 
 Black, Text[names[[#2]], #1]} &), 
 EdgeRenderingFunction -> ({AbsoluteThickness[2], Red, 
 Arrowheads[0.02], Arrow[#1, 0.05], Black, 
 Text[Round@ Abs[(lilnums[[#2[[1]], #2[[2]]]] + 
   lilnums[[#2[[2]], #2[[1]]]])], Mean[#1], 
  Background -> Yellow]} &), VertexLabeling -> True, 
 ImageSize -> 600,  
  PlotLabel -> Style["Plot Label", Bold, 14, FontFamily -> "Arial"]]
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


小智 5

EdgeLabels - >"EdgeWeight"仍然无法在8.0.4中运行,似乎不再出现在文档中.但是,这是一个有效的解决方案:

lilnums = {{0, 2., 1., 3., 0, 6.}, {0, 0, 1., 2., 0, 0}, {1., 8., 0, 2., 0, 2.},
  {10., 13., 7., 0, 0, 10.}, {0, 0, 0, 0, 0, 0}, {4., 1., 1., 2., 2., 0}}
names = {"AT", "AU", "CA", "CH", "CL", "ES"};
g = WeightedAdjacencyGraph[names, lilnums /. {0 -> \[Infinity]}, 
  VertexShapeFunction -> "Name" , ImagePadding -> 15];
SetProperty[g, EdgeLabels -> MapThread[#1 -> #2 &, 
  {EdgeList[g], PropertyValue[g, EdgeWeight]}]]
Run Code Online (Sandbox Code Playgroud)