如何用权重标记图边
警告! 我在 Mathematica v 8.0 是最酷的时候发布了这个问题。从版本 9.0.1 开始,该错误已得到解决
但是:
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:
However:
CompleteGraph[4,
EdgeWeight -> Range@6,
VertexShapeFunction -> "Name",
EdgeLabels -> "EdgeWeight"]
Results in:
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]
Better ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于常规的 GraphPlot,您需要使用 EdgeRenderingFunction 稍微复杂一些的解决方案(文档)。假设您有一个邻接矩阵,其中元素也是(方向)权重。
以下是顶点的一些标签,假设您正在绘制国际银行间风险敞口的网络图(原始图有更多国家/地区!)。
以下内容满足您的需要。技巧是使用零件规范内的
#2
部分引用回邻接矩阵,以引用nums
的正确元素,以及Mean[ #1]
将标签定位在边缘的中点。槽#1
似乎保存了顶点的坐标。For a regular
GraphPlot
, you will need a slightly more complicated solution usingEdgeRenderingFunction
(documentation). Suppose you have an adjacency matrix where the elements are also the (directional) weights.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!).
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 ofnums
, and theMean[#1]
to locate the label at the midpoint of the edge. The slot#1
seems to hold the coordinates of the vertices.边缘标签 -> “EdgeWeight”在 8.0.4 中仍然不起作用,并且似乎不再出现在文档中。然而,这是一种可行的解决方案:
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:
EdgeLabels
工作正常。EdgeWeights
不会。从贝利撒留的第二个示例中可能已经很明显看出问题出在
EdgeWeights
而不是EdgeLabels
这里有一些额外的内容证据。
EdgeLabels
非常高兴正确地显示各种标签。但是,当您要求 mma 显示"EdgeWeights"
时,无论您在那里存储了什么,它都会错误地显示 1。该错误并非
CompleteGraph
独有。Graph
和GridGraph
也有同样的问题。EdgeLabels
works fine.EdgeWeights
does not.It may already be obvious from Belisarius' second example that the problem lies with
EdgeWeights
notEdgeLabels
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.The bug is not unique to
CompleteGraph
.Graph
andGridGraph
have the same problem.解决办法很简单。升级到 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)
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).