DiGraph¶
全名: 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
一个有向图。
注意
与无向图不同,在此处指定给定边中顶点的顺序是相关的。
另请参阅
- 参数:
vertices (序列[可哈希]) – 顶点列表。必须是可哈希元素。
edges (序列[元组[可哈希, 可哈希]]) – 边列表,指定为元组
(u, v)
,其中u
和v
都是顶点。边从u
指向v
。labels (布尔值 | 字典) – 控制顶点是否被标记。如果为
False
(默认值),则顶点不被标记;如果为True
,则通过MathTex
使用其名称(在vertices
中指定)进行标记。另外,可以通过传入一个字典来指定自定义标签,该字典的键是顶点,值是相应的顶点标签(例如,通过Text
或Tex
渲染)。label_fill_color (字符串) – 设置当
labels
设置为True
时生成的默认标签的填充颜色。对于labels
的其他值无效。layout (布局名称 | 字典[可哈希, Point3DLike] | 布局函数) – 可以是
"spring"
(默认)、"circular"
、"kamada_kawai"
、"planar"
、"random"
、"shell"
、"spectral"
、"spiral"
、"tree"
和"partite"
之一,用于使用networkx
自动定位顶点(详见其文档),也可以是一个字典,为每个顶点(键)指定一个坐标(值),用于手动定位。layout_config (字典 | 无) – 仅适用于自动生成的布局。一个字典,其条目作为关键字参数传递给通过
networkx
的layout
指定的自动布局算法。tree
布局还接受一个特殊参数vertex_spacing
,作为关键字参数在layout_config
字典中传入。将元组(space_x, space_y)
作为此参数传入会覆盖layout_scale
的值,并确保顶点排列方式使得同一层中兄弟节点的中心在水平方向上至少相距space_x
单位,相邻层在垂直方向上相距space_y
单位。layout_scale (浮点数 | 元组[浮点数, 浮点数, 浮点数]) – 自动生成布局的比例:顶点将被排列,使其坐标位于区间
[-scale, scale]
内。某些布局接受元组(scale_x, scale_y)
,使第一个坐标位于区间[-scale_x, scale_x]
内,第二个坐标位于[-scale_y, scale_y]
内。默认值:2。vertex_type (类型[Mobject]) – 用于在场景中显示顶点的 mobject 类。
vertex_config (字典 | 无) – 可以是一个包含要传递给通过
vertex_type
指定的类的关键字参数的字典,也可以是一个字典,其键是顶点,值是包含与相应顶点相关的 mobject 的关键字参数的字典。vertex_mobjects (字典 | 无) – 一个字典,其键是顶点,值是作为顶点使用的 mobject。在此处传递顶点会覆盖顶点所有其他配置选项。
edge_type (类型[Mobject]) – 用于在场景中显示边的 mobject 类。
edge_config (字典 | 无) – 可以是一个包含要传递给通过
edge_type
指定的类的关键字参数的字典,也可以是一个字典,其键是边,值是包含与相应边相关的 mobject 的关键字参数的字典。您可以通过添加一个用于全局样式的tip_config
字典,或将该字典添加到特定的edge_config
中来进一步自定义尖端。partitions (序列[序列[可哈希]] | 无)
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()
由于此实现遵循标签边界,因此您也可以将其用于带有标签的无向移动图。
示例: UndirectedMovingDiGraph ¶
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_overrides (动画覆盖)
颜色
depth (深度)
mobject 的深度。
fill_color (填充颜色)
如果存在多种颜色(用于渐变),则返回第一种颜色
height (高度)
mobject 的高度。
n_points_per_curve (每条曲线的点数)
sheen_factor (光泽因子)
stroke_color (描边颜色)
width (宽度)
mobject 的宽度。
- _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))。
- 参数:
vertices (序列[可哈希])
edges (序列[元组[可哈希, 可哈希]])
labels (布尔值 | 字典)
label_fill_color (字符串)
layout (字面量['circular', 'kamada_kawai', 'partite', 'planar', 'random', 'shell', 'spectral', 'spiral', 'spring', 'tree'] | 字典[~collections.abc.可哈希, ~manim.typing.Point3DLike] | ~manim.mobject.graph.LayoutFunction)
layout_scale (浮点数 | 元组[浮点数, 浮点数, 浮点数])
layout_config (字典 | 无)
vertex_type (类型[Mobject])
vertex_config (字典 | 无)
vertex_mobjects (字典 | 无)
edge_type (类型[Mobject])
partitions (序列[序列[可哈希]] | 无)
root_vertex (可哈希 | 无)
edge_config (字典 | 无)
- 返回类型:
无