如何用权重标记图边

发布于 2024-12-04 07:28:53 字数 1174 浏览 3 评论 0原文

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

EdgeLabels 的帮助状态:

在此处输入图像描述

但是:

CompleteGraph[4,
 EdgeWeight -> Range@6,
 VertexShapeFunction -> "Name",
 EdgeLabels -> "EdgeWeight"]

结果为:

在此处输入图像描述

所以,没有边缘标签......我想这是一个错误。

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

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]

在此处输入图像描述

更好的想法吗?

Warning! I posted the question when Mathematica v 8.0 was the coolest kid. The bug has been solved as of version 9.0.1

The help for EdgeLabels states:

enter image description here

However:

CompleteGraph[4,
 EdgeWeight -> Range@6,
 VertexShapeFunction -> "Name",
 EdgeLabels -> "EdgeWeight"]

Results in:

enter image description here

So, no Edge Labels ... I guess it is a bug.

I used a nasty construct like:

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]

enter image description here

Better ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

妥活 2024-12-11 07:28:53

对于常规的 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}}

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

names = {"AT", "AU", "CA", "CH", "CL", "ES"}

以下内容满足您的需要。技巧是使用零件规范内的 #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"]]

在此处输入图像描述

For a regular GraphPlot, you will need a slightly more complicated solution using EdgeRenderingFunction (documentation). Suppose you have an adjacency matrix where the elements are also the (directional) weights.

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}}

Here are some labels for the vertices, supposing you are drawing network diagrams for international inter-bank exposures (the original has a lot more countries!).

names = {"AT", "AU", "CA", "CH", "CL", "ES"}

The following does what you need. The tricks are the reference back to the adjacency matrix using the parts of #2 inside the part specification, to reference the correct elements of nums, and the Mean[#1] to locate the label at the midpoint of the edge. The slot #1 seems to hold the coordinates of the vertices.

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"]]

enter image description here

み零 2024-12-11 07:28:53

边缘标签 -> “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]}]]

EdgeLabels -> "EdgeWeight" still doesn't work in 8.0.4 and no longer seems to be in the documentation. However, here is one solution that does work:

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]}]]
等待圉鍢 2024-12-11 07:28:53

EdgeLabels 工作正常。 EdgeWeights 不会。

从贝利撒留的第二个示例中可能已经很明显看出问题出在 EdgeWeights 而不是 EdgeLabels

这里有一些额外的内容证据。 EdgeLabels 非常高兴正确地显示各种标签。但是,当您要求 mma 显示 "EdgeWeights" 时,无论您在那里存储了什么,它都会错误地显示 1。

CompleteGraph[4, VertexShapeFunction -> "Name",
 EdgeLabels -> {
   UndirectedEdge[1, 2] -> "hello", 
   UndirectedEdge[1, 4] -> "goodbye", UndirectedEdge[2, 3] -> 55, 
   UndirectedEdge[3, 4] -> \[Pi]/2, 
   UndirectedEdge[4, 2] -> 
   "\!\(\*UnderoverscriptBox[\(\[Sum]\), \(i = 0\), \(26\)]\)(-1\!\(\
   \*SuperscriptBox[\()\), \(i\)]\)\!\(\*SuperscriptBox[\(\[Theta]\), \
   \(n - i\)]\)", UndirectedEdge[1, 3] -> {a, b, c}}]

EdgeWeights.png

该错误并非 CompleteGraph 独有。 GraphGridGraph 也有同样的问题。

EdgeLabels works fine. EdgeWeights does not.

It may already be obvious from Belisarius' second example that the problem lies with EdgeWeights not EdgeLabels

Here's some additional evidence. EdgeLabels very gladly displays a variety of labels correctly. But when you ask mma to display "EdgeWeights", it incorrectly displays 1's, no matter what you've stored there.

CompleteGraph[4, VertexShapeFunction -> "Name",
 EdgeLabels -> {
   UndirectedEdge[1, 2] -> "hello", 
   UndirectedEdge[1, 4] -> "goodbye", UndirectedEdge[2, 3] -> 55, 
   UndirectedEdge[3, 4] -> \[Pi]/2, 
   UndirectedEdge[4, 2] -> 
   "\!\(\*UnderoverscriptBox[\(\[Sum]\), \(i = 0\), \(26\)]\)(-1\!\(\
   \*SuperscriptBox[\()\), \(i\)]\)\!\(\*SuperscriptBox[\(\[Theta]\), \
   \(n - i\)]\)", UndirectedEdge[1, 3] -> {a, b, c}}]

EdgeWeights.png

The bug is not unique to CompleteGraph. Graph and GridGraph have the same problem.

孤独陪着我 2024-12-11 07:28:53

解决办法很简单。升级到 V 8.0.1 :)

至少这是我所拥有的并且它可以在那里工作。 (windows 7)

在此处输入图像描述

顺便说一句,我不知道边缘上的标签是否正确,但至少确实如此
将它们放在人物上,与您的图像不同)。

The solution is easy. Upgrade to V 8.0.1 :)

At least that is what I have and it works there. (windows 7)

enter image description here

Btw, I do not know if the labels on the edges are correct, but at least it does
put them on the figure, unlike your image).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文