问题描述
警告!我发布了问题,Mathematica v 8.0是最酷的孩子.该错误已从9.0.1版开始解决
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
但是:
CompleteGraph[4,
EdgeWeight -> Range@6,
VertexShapeFunction -> "Name",
EdgeLabels -> "EdgeWeight"]
结果:
所以,没有边缘标签...我想这是一个错误.
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]
更好的主意?
推荐答案
对于常规的GraphPlot
,您将需要使用EdgeRenderingFunction
(文档).假设您有一个邻接矩阵,其中的元素也是(方向)权重.
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"}
以下内容满足您的需求.技巧是使用零件规范内的#2
零件返回相邻矩阵,以引用nums
的正确元素,并使用Mean[#1]
将标签定位在边缘的中点.插槽#1
似乎保留了顶点的坐标.
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"]]
这篇关于如何用权重标注图形边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!