首页 > 解决方案 > 在 GraphPlot 中标记边

问题描述

我正在尝试使用 p 上的示例动画离散时间马尔可夫链的演变。30 of Kleinrock v. 1. 这很好用:

p = {
 {0, 3/4, 1/4},
 {1/4, 0, 3/4},
 {1/4, 1/4, 1/2}
}
Animate[BarChart[{0, 1, 0}.MatrixPower[p, n], PlotRange -> 1, 
        ChartLabels -> {"Kyoto", "Tokyo", "Osaka"}, 
        Epilog -> {Text[Style[n, Bold, 14], 
        Scaled[{.05, .9}], {-1, 0}]}], {n, 0, 10, 1}, AnimationRate -> 1,
        AnimationRunning -> False, RefreshRate -> 30]

所以接下来我想绘制状态图本身......我被困在试图以我想要的方式将标签放在顶点和边缘上。这将使用城市名称标记顶点:

cities = {"Kyoto", "Tokyo", "Osaka"}
GraphPlot[p, DirectedEdges -> True, VertexLabeling -> True,
          MultiedgeStyle -> All, SelfLoopStyle -> All, EdgeLabeling -> True,
          VertexRenderingFunction -> ({White, EdgeForm[Black], Disk[#, .1], 
          Black, Text[cities[[#2]], #1]} &)]

这给出了一个不太漂亮但有用的图形视图,其中边权重取自矩阵:

WeightedAdjacencyGraph[p, EdgeLabels -> "EdgeWeight"]

但我无法终生弄清楚如何将两者结合起来。

最终我的计划是在顶点的圆圈旁边画一个条,就像上面动画中的相应条一样,所以我真的需要一些绘图函数来修改顶点渲染。(我相信稍后我会带着更多的问题回来......)

fwiw,这是 Mac 上的 Mathematica 11.0.1.0。

帮助表示赞赏!

标签: wolfram-mathematica

解决方案


您可以使用EdgeRenderingFunction选项GraphPlot来控制边缘的绘图并添加权重。

首先,您需要转换pGraphPlot.

vl = Flatten[MapIndexed[{Rule @@ #2, #1} &, p, {-1}], 1];

然后使用以下EdgeRenderingFunction重量将绘制。

GraphPlot[vl,
 DirectedEdges -> True,
 MultiedgeStyle -> All,
 SelfLoopStyle -> All,
 EdgeRenderingFunction -> (
   {Darker@Red, Arrow[#1, 0.1],
     Black,
     Inset[#3,
      With[{len = Length@#1},
       If[len == 2,
        Mean[#1],
        #1[[Ceiling[len/2]]]
        ]],
      Background -> White]} &),
 VertexRenderingFunction -> ({White, EdgeForm[Black], Disk[#, .1], 
     Black, Text[cities[[#2]], #1]} &)
 ]

在此处输入图像描述

您可以Style使用#3参数使其更符合您的喜好。

希望这可以帮助。

此外,请查看Mathematica Stack Exchange以获取 Mathematica 专用论坛。


推荐阅读