我正在尝试使用Kleinrock v.1的第30页上的例子来演示离散时间马尔可夫链的演化。这非常有效:
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]
下一步,我想绘制状态图itself...and,我在尝试按我想要的方式将标签放在顶点和边上时遇到了困难。这将使用城市名称标记顶点:
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。
感谢您的帮助!
发布于 2018-06-17 03:40:49
您可以使用GraphPlot
的EdgeRenderingFunction
选项来控制边的绘图并添加权重。
首先,您需要将p
转换为GraphPlot
的顶点标签语法。
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]} &)
]
您可以对#3
参数执行Style
操作,使其更符合您的喜好。
希望这能有所帮助。
此外,请查看Mathematica Stack Exchange,以获得一个专门介绍Mathematica的论坛。
发布于 2018-06-17 22:15:46
结合上面的答案,这非常接近我想要的,尽管刷新率设置为30,但它在我的笔记本电脑上以丑陋的方式闪烁。不过,我的笔记本电脑早已过了退休年龄。
Animate[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, 0.12],
Pink, EdgeForm[Pink],
{svpi = {0, 1, 0}.MatrixPower[p, n];
Disk[#1, .1, {\[Pi]/2, \[Pi]/2 - 2 \[Pi] svpi[[#2]]}]},
Black, Text[cities[[#2]], #1]} &)],
{n, 0, 10, 1}, AnimationRate -> 1, AnimationRunning -> False,
RefreshRate -> 30]
https://stackoverflow.com/questions/50868240
复制相似问题