图与网络

Graph / DiGraph 把 networkx 的图论能力与 Manim 的动画系统结合起来:顶点、边都是普通 mobject,可以用任意动画逐个创建、移动、重连,非常适合讲解数据结构、状态机与网络拓扑。

Graph

class manim.Graph(vertices: Sequence[Hashable], edges: Sequence[tuple[Hashable, Hashable]], labels: bool | dict = False, label_fill_color: str = ManimColor('#000000'), layout: LayoutName | dict[Hashable, Point3DLike] | LayoutFunction = 'spring', layout_scale: float | tuple[float, float, float] = 2, layout_config: dict | None = None, vertex_type: type[Mobject] = <class 'manim.mobject.geometry.arc.Dot'>, vertex_config: dict | None = None, vertex_mobjects: dict | None = None, edge_type: type[Mobject] = <class 'manim.mobject.geometry.line.Line'>, partitions: Sequence[Sequence[Hashable]] | None = None, root_vertex: Hashable | None = None, edge_config: dict | None = None)

基类:GenericGraph

Graph 构造三个要素:顶点序列、边序列(顶点对的列表)和布局。布局可以是名字("circular" / "spring" / "kamada_kawai" / "planar" 等,由 networkx 计算)、坐标字典或自定义函数;vertex_config / edge_config 控制样式,labels=True 自动给顶点加标签。

G Graph Graph VMobject VMobject Graph->VMobject Mobject Mobject VMobject->Mobject object object Mobject->object

name

type

default

desc

vertices

Sequence[Hashable]

—

顶点集合(任意可哈希值)

edges

Sequence[tuple]

—

顶点对列表

layout

str | dict | callable

spring

布局名 / 坐标字典 / 布局函数

labels

bool | dict

False

顶点标签;True 时自动编号

vertex_config / edge_config

dict | None

None

顶点 / 边样式

partitions + root_vertex

—

None

分层布局的分区与根

Graph``(左)与 ``DiGraph``(右)使用同一组顶点和边;``labels=True 显示顶点编号。

查看源码 graph_network.py
from manim import *


class GraphDemo(Scene):
    def construct(self):
        vertices = [1, 2, 3, 4, 5]
        edges = [(1, 2), (2, 3), (3, 4), (4, 1), (2, 4), (1, 3)]
        g = Graph(vertices, edges, layout="circular", labels=True)
        self.play(Create(g), run_time=2)
        dg = DiGraph(vertices, edges, layout="circular", labels=True)
        dg.shift(RIGHT * 4.5)
        self.play(g.animate.shift(LEFT * 2.5), FadeIn(dg, shift=RIGHT), run_time=1.5)
        self.wait()

DiGraph

class manim.DiGraph(vertices: Sequence[Hashable], edges: Sequence[tuple[Hashable, Hashable]], labels: bool | dict = False, label_fill_color: str = ManimColor('#000000'), layout: LayoutName | dict[Hashable, Point3DLike] | LayoutFunction = 'spring', layout_scale: float | tuple[float, float, float] = 2, layout_config: dict | None = None, vertex_type: type[Mobject] = <class 'manim.mobject.geometry.arc.Dot'>, vertex_config: dict | None = None, vertex_mobjects: dict | None = None, edge_type: type[Mobject] = <class 'manim.mobject.geometry.line.Line'>, partitions: Sequence[Sequence[Hashable]] | None = None, root_vertex: Hashable | None = None, edge_config: dict | None = None)

基类:GenericGraph

DiGraph 是有向图:构造参数与 Graph 完全一致,区别在于每条边会自动加上箭头(可用 edge_config 里的 tip_config 调整箭头样式)。表示状态转移、依赖关系、流程图时用它。

G DiGraph DiGraph VMobject VMobject DiGraph->VMobject Mobject Mobject VMobject->Mobject object object Mobject->object

提示

图建好之后的动态修改(加顶点、换布局、改样式)直接对 Graph 对象调用相应方法后用 self.play 播放即可,官方文档的 MovingDiGraph 示例演示了移动顶点的标准写法。