add_edge(self, u_of_edge, v_of_edge, **attr)
The nodes u and v will be automatically added if they are not already in the graph.
Edge attributes can be specified with keywords or by directly accessing the edge's attribute dictionary. See examples below.
Adding an edge that already exists updates the edge data.
Many NetworkX algorithms designed for weighted graphs use an edge attribute (by default :None:None:`weight`) to hold a numerical value.
Nodes can be, for example, strings or numbers. Nodes must be hashable (and not None) Python objects.
Edge data (or labels or objects) can be assigned using keyword arguments.
Add an edge between u and v.
add_edges_from
add a collection of edges
The following all add the edge e=(1, 2) to graph G:
>>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
... e = (1, 2)
... G.add_edge(1, 2) # explicit two-node form
... G.add_edge(*e) # single edge as tuple of two nodes
... G.add_edges_from([(1, 2)]) # add edges from iterable container
Associate data to edges using keywords:
>>> G.add_edge(1, 2, weight=3)
... G.add_edge(1, 3, weight=7, capacity=15, length=342.7)
For non-string attribute keys, use subscript notation.
>>> G.add_edge(1, 2)See :
... G[1][2].update({0: 5})
... G.edges[1, 2].update({0: 5})
The following pages refer to to this document either explicitly or contain code examples using this.
networkx.classes.reportviews.EdgeDataViewnetworkx.algorithms.tree.mst.maximum_spanning_edgesnetworkx.classes.digraph.DiGraphnetworkx.algorithms.assortativity.mixing.attribute_mixing_dictnetworkx.algorithms.tree.mst.minimum_spanning_edgesnetworkx.algorithms.components.biconnected.biconnected_component_edgesnetworkx.classes.function.is_negatively_weightednetworkx.algorithms.summarization.snap_aggregationnetworkx.algorithms.tree.mst.maximum_spanning_treenetworkx.classes.function.number_of_selfloopsnetworkx.algorithms.connectivity.stoerwagner.stoer_wagnernetworkx.classes.graph.Graph.to_undirectednetworkx.readwrite.edgelist.write_weighted_edgelistnetworkx.linalg.attrmatrix.attr_matrixnetworkx.classes.function.freezenetworkx.classes.graph.Graphnetworkx.algorithms.isolate.is_isolatenetworkx.linalg.attrmatrix.attr_sparse_matrixnetworkx.classes.graph.Graph.add_edgenetworkx.algorithms.components.biconnected.biconnected_componentsnetworkx.classes.graph.Graph.add_edges_fromnetworkx.algorithms.tree.mst.minimum_spanning_treenetworkx.classes.function.nodes_with_selfloopsnetworkx.classes.graph.Graph.add_weighted_edges_fromnetworkx.classes.reportviews.EdgeViewnetworkx.convert_matrix.to_numpy_recarraynetworkx.readwrite.edgelist.write_edgelistnetworkx.algorithms.tree.recognition.is_treenetworkx.classes.reportviews.DiDegreeViewnetworkx.classes.graph.Graph.sizenetworkx.classes.graph.Graph.to_directednetworkx.algorithms.tree.recognition.is_forestnetworkx.convert_matrix.to_numpy_arraynetworkx.classes.digraph.DiGraph.add_edgenetworkx.algorithms.bipartite.projection.collaboration_weighted_projected_graphnetworkx.classes.reportviews.DegreeViewnetworkx.algorithms.bipartite.edgelist.write_edgelistnetworkx.algorithms.components.biconnected.articulation_pointsnetworkx.algorithms.components.biconnected.is_biconnectedHover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them