wolfram-mathematica - 在 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。
帮助表示赞赏!
解决方案
您可以使用EdgeRenderingFunction
选项GraphPlot
来控制边缘的绘图并添加权重。
首先,您需要转换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]} &)
]
您可以Style
使用#3
参数使其更符合您的喜好。
希望这可以帮助。
此外,请查看Mathematica Stack Exchange以获取 Mathematica 专用论坛。
推荐阅读
- java - 超过 40 次测试的 JdbcSQLException
- ios - 无法在 iPhone 上安装开发应用程序
- openstack - ubuntu 上的 microstack 没有注册 cinder 卷服务
- opengl - 如何从前向矢量计算俯仰和偏航值
- .net-core - 在 identotyserver 4 中的 API(资源)中引发错误
- react-native - GraphQl 生成语法错误:预期 $,找到 {]
- php - 如何向 ElasticSearch Aggregation (min/max) 添加条件?
- python - Apache/mod_wsgi ModuleNotFoundError
- android - 可绘制文件夹中的图片不起作用 - 为什么?
- azure - 如何修复 azure wiki 中的 toc 超链接锚滚动位置(当存在重复的锚时 - 例如:子主题)?