GenericGraph#
Qualified name: manim.mobject.graph.GenericGraph
- class GenericGraph(vertices, edges, labels=False, label_fill_color='#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)[source]#
Bases:
VMobject
Abstract base class for graphs (that is, a collection of vertices connected with edges).
Graphs can be instantiated by passing both a list of (distinct, hashable) vertex names, together with list of edges (as tuples of vertex names). See the examples for concrete implementations of this class for details.
Note
This implementation uses updaters to make the edges move with the vertices.
- Parameters
vertices (list[Hashable]) – A list of vertices. Must be hashable elements.
edges (list[tuple[Hashable, Hashable]]) – A list of edges, specified as tuples
(u, v)
where bothu
andv
are vertices.labels (bool | dict) – Controls whether or not vertices are labeled. If
False
(the default), the vertices are not labeled; ifTrue
they are labeled using their names (as specified invertices
) viaMathTex
. 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
orTex
).label_fill_color (str) – Sets the fill color of the default labels generated when
labels
is set toTrue
. Has no effect for other values oflabels
.layout (str | dict) – Either one of
"spring"
(the default),"circular"
,"kamada_kawai"
,"planar"
,"random"
,"shell"
,"spectral"
,"spiral"
,"tree"
, and"partite"
for automatic vertex positioning usingnetworkx
(see their documentation 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``. Thetree
layout also accepts a special parametervertex_spacing
passed as a keyword argument inside thelayout_config
dictionary. Passing a tuple(space_x, space_y)
as this argument overrides the value oflayout_scale
and ensures that vertices are arranged in a way such that the centers of siblings in the same layer are at leastspace_x
units apart horizontally, and neighboring layers are spacedspace_y
units vertically.layout_scale (float | tuple) – 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]) – 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]) – 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 (list[list[Hashable]] | None) –
root_vertex (Hashable | None) –
Methods
Add new edges to the graph.
Add a list of vertices to the graph.
Change the layout of this graph.
Remove several edges from the graph.
Remove several vertices from the graph.
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.
- add_edges(*edges, edge_type=<class 'manim.mobject.geometry.line.Line'>, edge_config=None, **kwargs)[source]#
Add new edges to the graph.
- Parameters
edges (tuple[Hashable, Hashable]) – Edges (as tuples of vertex identifiers) to be added. If a non-existing vertex is passed, a new vertex with default settings will be created. Create new vertices yourself beforehand to customize them.
edge_type (type[Mobject]) – The mobject class used for displaying edges in the scene.
edge_config (dict | None) – A dictionary either containing keyword arguments to be passed to the class specified via
edge_type
, or a dictionary whose keys are the edge tuples, and whose values are dictionaries containing keyword arguments to be passed for the construction of the corresponding edge.kwargs – Any further keyword arguments are passed to
add_vertices()
which is used to create new vertices in the passed edges.
- Returns
A group containing all newly added vertices and edges.
- Return type
- add_vertices(*vertices, positions=None, labels=False, label_fill_color='#000000', vertex_type=<class 'manim.mobject.geometry.arc.Dot'>, vertex_config=None, vertex_mobjects=None)[source]#
Add a list of vertices to the graph.
- Parameters
vertices (Hashable) – Hashable vertex identifiers.
positions (dict | None) – A dictionary specifying the coordinates where the new vertices should be added. If
None
, all vertices are created at the center of the graph.labels (bool) – Controls whether or not the vertex is labeled. If
False
(the default), the vertex is not labeled; ifTrue
it is labeled using its names (as specified invertex
) viaMathTex
. Alternatively, anyMobject
can be passed to be used as the label.label_fill_color (str) – Sets the fill color of the default labels generated when
labels
is set toTrue
. Has no effect for other values oflabels
.vertex_type (type[Mobject]) – The mobject class used for displaying vertices in the scene.
vertex_config (dict | None) – A dictionary containing keyword arguments to be passed to the class specified via
vertex_type
.vertex_mobjects (dict | None) – A dictionary whose keys are the vertex identifiers, and whose values are mobjects that should be used as vertices. Overrides all other vertex customization options.
self (Graph) –
- change_layout(layout='spring', layout_scale=2, layout_config=None, partitions=None, root_vertex=None)[source]#
Change the layout of this graph.
See the documentation of
Graph
for details about the keyword arguments.Examples
Example: ChangeGraphLayout ¶
from manim import * class ChangeGraphLayout(Scene): def construct(self): G = Graph([1, 2, 3, 4, 5], [(1, 2), (2, 3), (3, 4), (4, 5)], layout={1: [-2, 0, 0], 2: [-1, 0, 0], 3: [0, 0, 0], 4: [1, 0, 0], 5: [2, 0, 0]} ) self.play(Create(G)) self.play(G.animate.change_layout("circular")) self.wait()
- Parameters
layout (str | dict) –
layout_scale (float) –
layout_config (dict | None) –
partitions (list[list[Hashable]] | None) –
root_vertex (Hashable | None) –
- Return type
- classmethod from_networkx(nxgraph, **kwargs)[source]#
Build a
Graph
orDiGraph
from a givennetworkx
graph.- Parameters
nxgraph (nx.classes.graph.Graph | nx.classes.digraph.DiGraph) – A
networkx
graph or digraph.**kwargs – Keywords to be passed to the constructor of
Graph
.
Examples
Example: ImportNetworkxGraph ¶
from manim import * import networkx as nx nxgraph = nx.erdos_renyi_graph(14, 0.5) class ImportNetworkxGraph(Scene): def construct(self): G = Graph.from_networkx(nxgraph, layout="spring", layout_scale=3.5) self.play(Create(G)) self.play(*[G[v].animate.move_to(5*RIGHT*np.cos(ind/7 * PI) + 3*UP*np.sin(ind/7 * PI)) for ind, v in enumerate(G.vertices)]) self.play(Uncreate(G))
- remove_edges(*edges)[source]#
Remove several edges from the graph.
- Parameters
edges (tuple[Hashable]) – Edges to be removed from the graph.
- Returns
A group containing all removed edges.
- Return type
- remove_vertices(*vertices)[source]#
Remove several vertices from the graph.
- Parameters
vertices – Vertices to be removed from the graph.
Examples
>>> G = Graph([1, 2, 3], [(1, 2), (2, 3)]) >>> removed = G.remove_vertices(2, 3); removed VGroup(Line, Line, Dot, Dot) >>> G Undirected graph on 1 vertices and 0 edges