无向图属于数学中的图论这一学科,
所谓无向图G,就是由顶点集V(非空集合)和边集E(由V中元素构成的无序二元组的集合)组成的图,
可表示为G=(V,E)。
在无向图中,边没有方向,即从顶点A到顶点B的边与从顶点B到顶点A的边是相同的。
无向图简洁直观,常用于描述社交网络,交通网络以及电子电路等等。
在社交网络中,用户可以被视为顶点,而用户之间的关系则可以被视为边。
因为好友关系通常是双向的,没有明确的方向性,非常适合用无向图来表示。
在城市交通规划中,可以将各个路口或交通节点视为顶点,而将路口之间的道路或交通线路视为边。
因为道路通常是双向通行的,所以也适合用无向图来表示。
在电子电路设计中,可以将各个电子元件(如电阻、电容、电感等)视为顶点,而将元件之间的连接关系(如导线连接)视为边。
无向图可以表示这种电路结构,因为电路中的电流和信号通常是双向传输的。
下面介绍manim中绘制无向图的对象Graph。
1. 主要参数
无向图对象Graph主要参数有:
| 参数名称 |
类型 |
说明 |
| vertices |
list |
图的顶点列表 |
| edges |
list |
图的边列表,每个边 |
| labels |
dict |
顶点是否显示标签文本 |
| label_fill_color |
str |
标签的背景色 |
| layout |
str |
图中定点的布局方式 |
| layout_config |
dict |
配置如何布局图中各个顶点 |
| layout_scale |
float |
图各个顶点布局的比例 |
| vertex_type |
Mobject |
顶点的类型,不一定是点,也可以是manim中其他的对象 |
| vertex_config |
dict |
顶点相关的配置 |
| vertex_mobjects |
dict |
一系列的顶点对象 |
| edge_type |
Mobject |
边的类型,不一定是线,也可以是manim中其他的对象 |
| edge_config |
dict |
边相关的配置 |
| partitions |
list |
|
| root_vertex |
dict |
|
这些参数中,vertices和edges相关的参数(比如xxx_type,xxx_config)比较好理解。
labels参数设置是否需要显示顶点的标签,默认是把vertices的数值作为标签的内容。
layout参数内置了多种现成的布局方式:
'circular',
'kamada_kawai'
'partite'
'planar'
'random'
'shell'
'spectral'
'spiral'
'spring'
'tree'
layout_config参数可以对上面现成布局方式的进行微调。
最后两个参数partitions和root_vertex比较特殊,
partitions只能在layout设置为'partite'时使用,用来生成层状的图(比如描述神经网络的图),
partitions用来设置每一层包含哪些顶点;
root_vertex只能在layout设置为'tree'时使用,用来树状图,
root_vertex用来设置树的根节点。
后面的示例会演示如何使用partitions和root_vertex来生成层状和树状的无向图。
2. 主要方法
无向图Graph的方法主要用来动态改变无向图,比如添加或删除顶点和边。
| 名称 |
说明 |
| add_edges |
增加无向图的边 |
| add_vertices |
增加无向图的顶点 |
| remove_edges |
删除无向图的边 |
| remove_vertices |
删除无向图的顶点 |
| change_layout |
动态改表无向图的结构 |
| from_networkx |
从networkx来生成无向图 |
networkx是另一个常用的Python库,用于创建、操作和研究复杂网络的结构。
Graph对象可以直接根据networkx的对象生成图。
3. 使用示例
下面通过示例了解Graph对象的使用。
3.1. 顶点的配置
顶点默认是Dot对象,可以设置其大小和颜色,也可以添加标签,也就是在顶点中显示文字。
更进一步,设置可以改变顶点的形状,使用manim中的其他几何对象来作为图的顶点。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| from manim import * FONT = "STLiti" V = [0, 1, 2] E = [(0, 1), (0, 2), (1, 2)] LAB = {i: Text(str(i), font=FONT, font_size=28, color=BLACK) for i in V} class GraphExample(Scene): def construct(self): head = Text("顶点的配置:颜色 / 标签 / 星形", font=FONT, font_size=32, color=YELLOW).to_edge(UP) g1 = Graph(V, E, layout="circular", layout_scale=1.0, vertex_config={0: {"color": RED}, 1: {"color": GREEN}, 2: {"color": BLUE}}) g2 = Graph(V, E, layout="circular", layout_scale=1.0, labels=LAB, vertex_config={"color": WHITE, "radius": 0.28}) g3 = Graph(V, E, layout="circular", layout_scale=1.0, vertex_type=Star, vertex_config={"outer_radius": 0.22, "color": YELLOW}) row = Group(g1, g2, g3).arrange(RIGHT, buff=1.8).scale(1.25) caps = Group(*[Text(s, font=FONT, font_size=24, color=YELLOW_C) for s in ("不同颜色的顶点", "带标签的顶点", "星形的顶点")]) for i, cap in enumerate(caps): cap.next_to(row[i], DOWN, buff=0.45) self.add(head) self.play(FadeIn(g1), FadeIn(caps[0]), run_time=1.0) self.play(FadeIn(g2), FadeIn(caps[1]), run_time=1.0) self.play(FadeIn(g3), FadeIn(caps[2]), run_time=1.0) self.wait(2.6)
|
下载 Python 脚本
3.2. 边的配置
无向图的边也和顶点一样,可以设置颜色,粗细等属性,此外还可以将默认的直线改成虚线。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| from manim import * FONT = "STLiti" V = [0, 1, 2] E = [(0, 1), (0, 2), (1, 2)] class GraphExample(Scene): def construct(self): head = Text("边的配置:颜色 / 粗细 / 虚线", font=FONT, font_size=32, color=YELLOW).to_edge(UP) g1 = Graph(V, E, layout="circular", layout_scale=1.0, edge_config={(0, 1): {"color": RED}, (0, 2): {"color": GREEN}, (1, 2): {"color": BLUE}}) g2 = Graph(V, E, layout="circular", layout_scale=1.0, edge_config={(0, 1): {"stroke_width": 2}, (0, 2): {"stroke_width": 8}, (1, 2): {"stroke_width": 14}}) g3 = Graph(V, E, layout="circular", layout_scale=1.0, edge_type=DashedLine, edge_config={"dash_length": 0.15}) g3.clear_updaters() row = Group(g1, g2, g3).arrange(RIGHT, buff=1.8).scale(1.25) caps = Group(*[Text(s, font=FONT, font_size=24, color=YELLOW_C) for s in ("不同颜色的边", "不同粗细的边", "虚线的边")]) for i, cap in enumerate(caps): cap.next_to(row[i], DOWN, buff=0.45) self.add(head) self.play(FadeIn(g1), FadeIn(caps[0]), run_time=1.0) self.play(FadeIn(g2), FadeIn(caps[1]), run_time=1.0) self.add(g3) self.play(FadeIn(caps[2]), run_time=1.0) self.wait(2.6)
|
下载 Python 脚本
3.3. 内置的layout
上面的示例中,我们只能控制无向图的顶点和边的数量,而无法控制它的形状和布局,
manim为我们内置了多种不同的布局方式,直接使用这些内置的布局方式可以节约很多时间。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| from manim import * FONT = "STLiti" V = [1, 2, 3, 4, 5, 6] E = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 1), (5, 1), (1, 3), (3, 5)] NAMES = ["spring", "circular", "kamada_kawai", "planar", "random", "shell", "spectral", "spiral"] class GraphExample(Scene): def construct(self): head = VGroup(Text("Graph ", font="Consolas", font_size=32, color=YELLOW), Text("内置的八种布局", font=FONT, font_size=32, color=YELLOW)).arrange(RIGHT, buff=0.1).to_edge(UP) cards = [] for n in NAMES: g = Graph(V, E, layout=n, layout_scale=0.75) cap = Text(n, font="Consolas", font_size=20, color=GREEN).next_to(g, DOWN, buff=0.12) cards.append(VGroup(g, cap)) Group(*cards).arrange_in_grid(rows=2, cols=4, buff=(0.9, 1.0)).scale(1.2) self.add(head) for c in cards: self.play(FadeIn(c[0]), FadeIn(c[1]), run_time=0.45) self.wait(2.0)
|
下载 Python 脚本
3.4. 层状图
层状图的布局需要配合参数partitions一起使用,partitions中决定每一层中有哪些顶点。
神经网络的结构图就是一个典型的层状图。
下面的示例中,通过partitions参数中定义了4个层。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| from manim import * FONT = "STLiti" V = list(range(9)) E = [(0, 2), (0, 3), (0, 4), (1, 3), (1, 4), (2, 5), (2, 6), (3, 5), (3, 6), (3, 7), (4, 6), (4, 8), (5, 7), (5, 8), (6, 7), (6, 8)] PARTS = [[0, 1], [2, 3, 4], [5, 6], [7, 8]] COL = [RED, GREEN, BLUE, YELLOW, PURPLE, ORANGE] LAB = {i: Text(str(i), font=FONT, font_size=26, color=BLACK) for i in V} class GraphExample(Scene): def construct(self): head = VGroup(Text("层状图:", font=FONT, font_size=32, color=YELLOW), Text("partitions", font="Consolas", font_size=30, color=YELLOW), Text(" 四层", font=FONT, font_size=32, color=YELLOW)).arrange(RIGHT, buff=0.08).to_edge(UP) cfg = {ee: {"color": COL[i % len(COL)], "stroke_width": 6} for i, ee in enumerate(E)} g = Graph(V, E, layout="partite", partitions=PARTS, layout_scale=3.0, labels=LAB, vertex_config={"color": WHITE, "radius": 0.30}, edge_config=cfg).scale(1.2) self.play(FadeIn(head), run_time=0.6) self.play(FadeIn(VGroup(*g.vertices.values())), run_time=1.2) self.play(Create(VGroup(*g.edges.values())), run_time=1.8) self.wait(2.0)
|
下载 Python 脚本
3.5. 树状图
树状图的布局需要配合参数root_vertex一起使用,root_vertex定义了树的根顶点是哪个。
下面的示例中,先设置root_vertex为顶点0,再把root_vertex改为顶点2。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| from manim import * FONT = "STLiti" V = list(range(9)) E = [(0, 1), (0, 2), (0, 3), (1, 4), (1, 5), (2, 6), (2, 7), (3, 8)] COL = [YELLOW, RED, GREEN, BLUE, PURPLE, ORANGE, TEAL, MAROON] ECFG = {e: {"color": COL[i % len(COL)], "stroke_width": 5} for i, e in enumerate(E)} CFG = {"color": WHITE, "radius": 0.30} class GraphExample(Scene): def construct(self): lab1 = {i: Text(str(i), font=FONT, font_size=26, color=BLACK) for i in V} g = Graph(V, E, layout="tree", root_vertex=0, layout_scale=3.0, labels=lab1, vertex_config=CFG, edge_config=ECFG).scale(1.2) head = Text("root_vertex = 0", font="Consolas", font_size=30, color=YELLOW).to_corner(UL, buff=0.4) self.play(FadeIn(head), FadeIn(g), run_time=1.4) self.wait(0.8) lab2 = {i: Text(str(i), font=FONT, font_size=26, color=BLACK) for i in V} g2 = Graph(V, E, layout="tree", root_vertex=2, layout_scale=3.0, labels=lab2, vertex_config=CFG, edge_config=ECFG).scale(1.2) head2 = Text("root_vertex = 2", font="Consolas", font_size=30, color=YELLOW).to_corner(UL, buff=0.4) self.play(Transform(g, g2), run_time=1.8) self.remove(head) self.add(head2) self.wait(1.6)
|
下载 Python 脚本
4. 附件
五个示例的完整脚本如下,与上方代码块、演示视频逐字一致;用 manim -qh 脚本.py 本地渲染即可复现。
主观感受,仅供参考。本文采用 本文采用 CC BY-NC-SA 4.0 许可 协议,
完整转载请注明来自
数学动画引擎。