File size: 11,210 Bytes
dc00adb | 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | # Graph
Qualified name: `manim.mobject.graph.Graph`
### *class* Graph(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)
Bases: [`GenericGraph`](manim.mobject.graph.GenericGraph.md#manim.mobject.graph.GenericGraph)
An undirected graph (vertices connected with edges).
The graph comes with an updater which makes the edges stick to
the vertices when moved around. See [`DiGraph`](manim.mobject.graph.DiGraph.md#manim.mobject.graph.DiGraph) for
a version with directed edges.
#### SEE ALSO
[`GenericGraph`](manim.mobject.graph.GenericGraph.md#manim.mobject.graph.GenericGraph)
* **Parameters:**
* **vertices** (*Sequence* *[**Hashable* *]*) β A list of vertices. Must be hashable elements.
* **edges** (*Sequence* *[**tuple* *[**Hashable* *,* *Hashable* *]* *]*) β A list of edges, specified as tuples `(u, v)` where both `u`
and `v` are vertices. The vertex order is irrelevant.
* **labels** (*bool* *|* *dict*) β Controls whether or not vertices are labeled. If `False` (the default),
the vertices are not labeled; if `True` they are labeled using their
names (as specified in `vertices`) via [`MathTex`](manim.mobject.text.tex_mobject.MathTex.md#manim.mobject.text.tex_mobject.MathTex). Alternatively,
custom labels can be specified by passing a dictionary whose keys are
the vertices, and whose values are the corresponding vertex labels
(rendered via, e.g., [`Text`](manim.mobject.text.text_mobject.Text.md#manim.mobject.text.text_mobject.Text) or [`Tex`](manim.mobject.text.tex_mobject.Tex.md#manim.mobject.text.tex_mobject.Tex)).
* **label_fill_color** (*str*) β Sets the fill color of the default labels generated when `labels`
is set to `True`. Has no effect for other values of `labels`.
* **layout** (*LayoutName* *|* *dict* *[**Hashable* *,* [*Point3DLike*](manim.typing.md#manim.typing.Point3DLike) *]* *|* [*LayoutFunction*](manim.mobject.graph.LayoutFunction.md#manim.mobject.graph.LayoutFunction)) β Either one of `"spring"` (the default), `"circular"`, `"kamada_kawai"`,
`"planar"`, `"random"`, `"shell"`, `"spectral"`, `"spiral"`, `"tree"`, and `"partite"`
for automatic vertex positioning using `networkx`
(see [their documentation](https://networkx.org/documentation/stable/reference/drawing.html#module-networkx.drawing.layout)
for more details), or a dictionary specifying a coordinate (value)
for each vertex (key) for manual positioning.
* **layout_config** (*dict* *|* *None*) β Only for automatically generated layouts. A dictionary whose entries
are passed as keyword arguments to the automatic layout algorithm
specified via `layout` of `networkx`.
The `tree` layout also accepts a special parameter `vertex_spacing`
passed as a keyword argument inside the `layout_config` dictionary.
Passing a tuple `(space_x, space_y)` as this argument overrides
the value of `layout_scale` and ensures that vertices are arranged
in a way such that the centers of siblings in the same layer are
at least `space_x` units apart horizontally, and neighboring layers
are spaced `space_y` units vertically.
* **layout_scale** (*float* *|* *tuple* *[**float* *,* *float* *,* *float* *]*) β The scale of automatically generated layouts: the vertices will
be arranged such that the coordinates are located within the
interval `[-scale, scale]`. Some layouts accept a tuple `(scale_x, scale_y)`
causing the first coordinate to be in the interval `[-scale_x, scale_x]`,
and the second in `[-scale_y, scale_y]`. Default: 2.
* **vertex_type** (*type* *[*[*Mobject*](manim.mobject.mobject.Mobject.md#manim.mobject.mobject.Mobject) *]*) β The mobject class used for displaying vertices in the scene.
* **vertex_config** (*dict* *|* *None*) β Either a dictionary containing keyword arguments to be passed to
the class specified via `vertex_type`, or a dictionary whose keys
are the vertices, and whose values are dictionaries containing keyword
arguments for the mobject related to the corresponding vertex.
* **vertex_mobjects** (*dict* *|* *None*) β A dictionary whose keys are the vertices, and whose values are
mobjects to be used as vertices. Passing vertices here overrides
all other configuration options for a vertex.
* **edge_type** (*type* *[*[*Mobject*](manim.mobject.mobject.Mobject.md#manim.mobject.mobject.Mobject) *]*) β The mobject class used for displaying edges in the scene.
* **edge_config** (*dict* *|* *None*) β Either a dictionary containing keyword arguments to be passed
to the class specified via `edge_type`, or a dictionary whose
keys are the edges, and whose values are dictionaries containing
keyword arguments for the mobject related to the corresponding edge.
* **partitions** (*Sequence* *[**Sequence* *[**Hashable* *]* *]* *|* *None*)
* **root_vertex** (*Hashable* *|* *None*)
### Examples
First, we create a small graph and demonstrate that the edges move
together with the vertices.
There are several automatic positioning algorithms to choose from:
Vertices can also be positioned manually:
The vertices in graphs can be labeled, and configurations for vertices
and edges can be modified both by default and for specific vertices and
edges.
#### NOTE
In `edge_config`, edges can be passed in both directions: if
`(u, v)` is an edge in the graph, both `(u, v)` as well
as `(v, u)` can be used as keys in the dictionary.
You can also lay out a partite graph on columns by specifying
a list of the vertices on each side and choosing the partite layout.
#### NOTE
All vertices in your graph which are not listed in any of the partitions
are collected in their own partition and rendered in the rightmost column.
The representation of a linear artificial neural network is facilitated
by the use of the partite layout and defining partitions for each layer.
The custom tree layout can be used to show the graph
by distance from the root vertex. You must pass the root vertex
of the tree.
The following code sample illustrates the use of the `vertex_spacing`
layout parameter specific to the `"tree"` layout. As mentioned
above, setting `vertex_spacing` overrides the specified value
for `layout_scale`, and as such it is harder to control the size
of the mobject. However, we can adjust the captured frame and
zoom out by using a [`MovingCameraScene`](manim.scene.moving_camera_scene.MovingCameraScene.md#manim.scene.moving_camera_scene.MovingCameraScene):
```default
class LargeTreeGeneration(MovingCameraScene):
DEPTH = 4
CHILDREN_PER_VERTEX = 3
LAYOUT_CONFIG = {"vertex_spacing": (0.5, 1)}
VERTEX_CONF = {"radius": 0.25, "color": BLUE_B, "fill_opacity": 1}
def expand_vertex(self, g, vertex_id: str, depth: int):
new_vertices = [
f"{vertex_id}/{i}" for i in range(self.CHILDREN_PER_VERTEX)
]
new_edges = [(vertex_id, child_id) for child_id in new_vertices]
g.add_edges(
*new_edges,
vertex_config=self.VERTEX_CONF,
positions={
k: g.vertices[vertex_id].get_center() + 0.1 * DOWN
for k in new_vertices
},
)
if depth < self.DEPTH:
for child_id in new_vertices:
self.expand_vertex(g, child_id, depth + 1)
return g
def construct(self):
g = Graph(["ROOT"], [], vertex_config=self.VERTEX_CONF)
g = self.expand_vertex(g, "ROOT", 1)
self.add(g)
self.play(
g.animate.change_layout(
"tree",
root_vertex="ROOT",
layout_config=self.LAYOUT_CONFIG,
)
)
self.play(self.camera.auto_zoom(g, margin=1), run_time=0.5)
```
### Methods
| `update_edges` | |
|------------------|----|
### Attributes
| `animate` | Used to animate the application of any method of `self`. |
|-----------------------|------------------------------------------------------------------------|
| `animation_overrides` | |
| `color` | |
| `depth` | The depth of the mobject. |
| `fill_color` | If there are multiple colors (for gradient) this returns the first one |
| `height` | The height of the mobject. |
| `n_points_per_curve` | |
| `sheen_factor` | |
| `stroke_color` | |
| `width` | The width of the mobject. |
#### *static* \_empty_networkx_graph()
Return an empty networkx graph for the given graph type.
* **Return type:**
*Graph*
#### \_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)
Initialize self. See help(type(self)) for accurate signature.
* **Parameters:**
* **vertices** (*Sequence* *[**Hashable* *]*)
* **edges** (*Sequence* *[**tuple* *[**Hashable* *,* *Hashable* *]* *]*)
* **labels** (*bool* *|* *dict*)
* **label_fill_color** (*str*)
* **layout** (*Literal* *[* *'circular'* *,* *'kamada_kawai'* *,* *'partite'* *,* *'planar'* *,* *'random'* *,* *'shell'* *,* *'spectral'* *,* *'spiral'* *,* *'spring'* *,* *'tree'* *]* *|* *dict* *[* *~collections.abc.Hashable* *,* *~manim.typing.Point3DLike* *]* *|* *~manim.mobject.graph.LayoutFunction*)
* **layout_scale** (*float* *|* *tuple* *[**float* *,* *float* *,* *float* *]*)
* **layout_config** (*dict* *|* *None*)
* **vertex_type** (*type* *[*[*Mobject*](manim.mobject.mobject.Mobject.md#manim.mobject.mobject.Mobject) *]*)
* **vertex_config** (*dict* *|* *None*)
* **vertex_mobjects** (*dict* *|* *None*)
* **edge_type** (*type* *[*[*Mobject*](manim.mobject.mobject.Mobject.md#manim.mobject.mobject.Mobject) *]*)
* **partitions** (*Sequence* *[**Sequence* *[**Hashable* *]* *]* *|* *None*)
* **root_vertex** (*Hashable* *|* *None*)
* **edge_config** (*dict* *|* *None*)
* **Return type:**
None
#### \_populate_edge_dict(edges, edge_type)
Helper method for populating the edges of the graph.
* **Parameters:**
* **edges** (*list* *[**tuple* *[**Hashable* *,* *Hashable* *]* *]*)
* **edge_type** (*type* *[*[*Mobject*](manim.mobject.mobject.Mobject.md#manim.mobject.mobject.Mobject) *]*)
|