The preferred way of converting data to a NetworkX graph is through the graph constructor. The constructor calls the ~networkx.convert.to_networkx_graph
function which attempts to guess the input type and convert it automatically.
Functions to convert NetworkX graphs to and from common data containers like numpy arrays, scipy sparse arrays, and pandas DataFrames.
Functions to convert NetworkX graphs to and from common data containers like numpy arrays, scipy sparse arrays, and pandas DataFrames.
The preferred way of converting data to a NetworkX graph is through the graph constructor. The constructor calls the ~networkx.convert.to_networkx_graph
function which attempts to guess the input type and convert it automatically.
Create a 10 node random graph from a numpy array
>>> import numpy as np >>> rng = np.random.default_rng() >>> a = rng.integers(low=0, high=2, size=(10, 10)) >>> DG = nx.from_numpy_array(a, create_using=nx.DiGraph)
or equivalently:
>>> DG = nx.DiGraph(a)
which calls from_numpy_array
internally based on the type of a
.
nx_agraph, nx_pydot
Hover 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