有向图¶
限定名称: manim.mobject.graph.DiGraph
- class DiGraph(vertices, edges, labels=False, label_fill_color=ManimColor('#000000'), layout='spring', layout_scale=2, layout_config=None, vertex_type=<class 'manim.mobject.geometry.arc.Dot'>, vertex_config=None, vertex_mobjects=None, edge_type=<class 'manim.mobject.geometry.line.Line'>, partitions=None, root_vertex=None, edge_config=None)[来源]¶
基础:
GenericGraph一个有向图。
注意
与无向图相比,这里指定给定边中顶点的顺序是相关的。
另请参阅
- Parameters:
顶点 (列表[可哈希]) – 顶点列表。必须是可哈希的元素。
edges (list[tuple[Hashable, Hashable]]) – 边的列表,指定为元组
(u, v),其中u和v都是顶点。边从u指向v。labels (bool | dict) – 控制顶点是否被标记。如果为
False(默认值), 顶点不会被标记;如果为True,则使用它们的名称(在vertices中指定)通过MathTex进行标记。另外, 可以通过传递一个字典来指定自定义标签,字典的键是顶点,值是对应的顶点标签 (通过例如文本或Tex渲染)。label_fill_color (str) – 设置当
labels设置为True时生成的默认标签的填充颜色。对于labels的其他值没有影响。布局 (布局名称 | 字典[可哈希, 三维点] | 布局函数) – 可以是以下之一:
"spring"(默认)、"circular"、"kamada_kawai"、"planar"、"random"、"shell"、"spectral"、"spiral"、"tree"和"partite", 用于使用networkx自动定位顶点 (更多详情请参阅 他们的文档), 或者是一个字典,指定每个顶点(键)的坐标(值)用于手动定位。layout_config (dict | None) – 仅用于自动生成的布局。一个字典,其条目作为关键字参数传递给通过
layout指定的自动布局算法。tree布局还接受一个特殊参数vertex_spacing,作为layout_config字典中的关键字参数传递。 传递一个元组(space_x, space_y)作为此参数会覆盖layout_scale的值,并确保顶点以这样的方式排列:同一层中的兄弟节点的中心至少水平相距space_x单位,相邻层垂直相距space_y单位。layout_scale (float | tuple[float, float, float]) – 自动生成布局的比例:顶点将被安排,使得坐标位于区间
[-scale, scale]内。某些布局接受一个元组(scale_x, scale_y),使得第一个坐标位于区间[-scale_x, scale_x],第二个坐标位于[-scale_y, scale_y]。默认值:2。vertex_type (type[Mobject]) – 用于在场景中显示顶点的mobject类。
vertex_config (dict | None) – 可以是一个包含关键字参数的字典,这些参数将传递给通过
vertex_type指定的类,或者是一个字典,其键是顶点,值是与相应顶点相关的mobject的关键字参数字典。vertex_mobjects (dict | None) – 一个字典,其键是顶点,值是用于顶点的mobjects。在此传递顶点将覆盖顶点的所有其他配置选项。
edge_type (type[Mobject]) – 用于在场景中显示边的mobject类。
edge_config (dict | None) – 可以是一个包含关键字参数的字典,这些参数将通过
edge_type传递给指定的类,或者是一个字典,其键是边,值是与相应边相关的mobject的关键字参数。您可以通过添加tip_config字典来进行全局样式定制,或者将字典添加到特定的edge_config中。partitions (列表[列表[Hashable]] | 无)
root_vertex (可哈希的 | 无)
示例
示例:MovingDiGraph ¶
from manim import * class MovingDiGraph(Scene): def construct(self): vertices = [1, 2, 3, 4] edges = [(1, 2), (2, 3), (3, 4), (1, 3), (1, 4)] g = DiGraph(vertices, edges) self.add(g) self.play( g[1].animate.move_to([1, 1, 1]), g[2].animate.move_to([-1, 1, 2]), g[3].animate.move_to([1, -1, -1]), g[4].animate.move_to([-1, -1, 0]), ) self.wait()
class MovingDiGraph(Scene): def construct(self): vertices = [1, 2, 3, 4] edges = [(1, 2), (2, 3), (3, 4), (1, 3), (1, 4)] g = DiGraph(vertices, edges) self.add(g) self.play( g[1].animate.move_to([1, 1, 1]), g[2].animate.move_to([-1, 1, 2]), g[3].animate.move_to([1, -1, -1]), g[4].animate.move_to([-1, -1, 0]), ) self.wait()您可以全局或局部自定义边和箭头尖端。
示例:CustomDiGraph ¶
from manim import * class CustomDiGraph(Scene): def construct(self): vertices = [i for i in range(5)] edges = [ (0, 1), (1, 2), (3, 2), (3, 4), ] edge_config = { "stroke_width": 2, "tip_config": { "tip_shape": ArrowSquareTip, "tip_length": 0.15, }, (3, 4): { "color": RED, "tip_config": {"tip_length": 0.25, "tip_width": 0.25} }, } g = DiGraph( vertices, edges, labels=True, layout="circular", edge_config=edge_config, ).scale(1.4) self.play(Create(g)) self.wait()
class CustomDiGraph(Scene): def construct(self): vertices = [i for i in range(5)] edges = [ (0, 1), (1, 2), (3, 2), (3, 4), ] edge_config = { "stroke_width": 2, "tip_config": { "tip_shape": ArrowSquareTip, "tip_length": 0.15, }, (3, 4): { "color": RED, "tip_config": {"tip_length": 0.25, "tip_width": 0.25} }, } g = DiGraph( vertices, edges, labels=True, layout="circular", edge_config=edge_config, ).scale(1.4) self.play(Create(g)) self.wait()由于此实现尊重标签边界,您还可以将其用于带有标签的无向移动图。
示例:无向移动图 ¶
from manim import * class UndirectedMovingDiGraph(Scene): def construct(self): vertices = [i for i in range(5)] edges = [ (0, 1), (1, 2), (3, 2), (3, 4), ] edge_config = { "stroke_width": 2, "tip_config": {"tip_length": 0, "tip_width": 0}, (3, 4): {"color": RED}, } g = DiGraph( vertices, edges, labels=True, layout="circular", edge_config=edge_config, ).scale(1.4) self.play(Create(g)) self.wait() self.play( g[1].animate.move_to([1, 1, 1]), g[2].animate.move_to([-1, 1, 2]), g[3].animate.move_to([-1.5, -1.5, -1]), g[4].animate.move_to([1, -2, -1]), ) self.wait()
class UndirectedMovingDiGraph(Scene): def construct(self): vertices = [i for i in range(5)] edges = [ (0, 1), (1, 2), (3, 2), (3, 4), ] edge_config = { "stroke_width": 2, "tip_config": {"tip_length": 0, "tip_width": 0}, (3, 4): {"color": RED}, } g = DiGraph( vertices, edges, labels=True, layout="circular", edge_config=edge_config, ).scale(1.4) self.play(Create(g)) self.wait() self.play( g[1].animate.move_to([1, 1, 1]), g[2].animate.move_to([-1, 1, 2]), g[3].animate.move_to([-1.5, -1.5, -1]), g[4].animate.move_to([1, -2, -1]), ) self.wait()方法
更新边以粘附在其对应的顶点上。
属性
animate用于动画化
self的任何方法的应用。animation_overridescolordepthmobject的深度。
fill_color如果有多种颜色(用于渐变),则返回第一个颜色
heightmobject的高度。
n_points_per_curvesheen_factorstroke_colorwidthmobject的宽度。
- _original__init__(vertices, edges, labels=False, label_fill_color=ManimColor('#000000'), layout='spring', layout_scale=2, layout_config=None, vertex_type=<class 'manim.mobject.geometry.arc.Dot'>, vertex_config=None, vertex_mobjects=None, edge_type=<class 'manim.mobject.geometry.line.Line'>, partitions=None, root_vertex=None, edge_config=None)¶
初始化自身。有关准确的签名,请参阅 help(type(self))。
- Parameters:
顶点 (列表[可哈希])
edges (列表[元组[Hashable, Hashable]])
标签 (布尔 | 字典)
label_fill_color (str)
布局 (字面量['circular', 'kamada_kawai', 'partite', 'planar', 'random', 'shell', 'spectral', 'spiral', 'spring', 'tree'] | 字典[~typing.Hashable, ~manim.typing.Point3D] | ~manim.mobject.graph.LayoutFunction)
layout_scale (float | tuple[float, float, float])
layout_config (dict | None)
vertex_type (类型[Mobject])
vertex_config (字典 | 无)
vertex_mobjects (字典 | 无)
edge_type (类型[Mobject])
partitions (列表[列表[Hashable]] | 无)
root_vertex (可哈希的 | 无)
edge_config (dict | None)
- Return type:
无