Warning
This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.
Converting to and from other data formats¶
To NetworkX Graph¶
Functions to convert NetworkX graphs to and from other formats.
The preferred way of converting data to a NetworkX graph is through the graph constuctor. The constructor calls the to_networkx_graph() function which attempts to guess the input type and convert it automatically.
Examples¶
Create a graph with a single edge from a dictionary of dictionaries
>>> d={0: {1: 1}} # dict-of-dicts single edge (0,1)
>>> G=nx.Graph(d)
See Also¶
nx_pygraphviz, nx_pydot
to_networkx_graph(data[, create_using, ...]) | Make a NetworkX graph from a known data structure. |
Dictionaries¶
to_dict_of_dicts(G[, nodelist, edge_data]) | Return adjacency representation of graph as a dictionary of dictionaries. |
from_dict_of_dicts(d[, create_using, ...]) | Return a graph from a dictionary of dictionaries. |
Lists¶
to_dict_of_lists(G[, nodelist]) | Return adjacency representation of graph as a dictionary of lists. |
from_dict_of_lists(d[, create_using]) | Return a graph from a dictionary of lists. |
to_edgelist(G[, nodelist]) | Return a list of edges in the graph. |
from_edgelist(edgelist[, create_using]) | Return a graph from a list of edges. |
Numpy¶
Functions to convert NetworkX graphs to and from numpy/scipy matrices.
The preferred way of converting data to a NetworkX graph is through the graph constuctor. The constructor calls the to_networkx_graph() function which attempts to guess the input type and convert it automatically.
Examples¶
Create a 10 node random graph from a numpy matrix
>>> import numpy
>>> a = numpy.reshape(numpy.random.random_integers(0,1,size=100),(10,10))
>>> D = nx.DiGraph(a)
or equivalently
>>> D = nx.to_networkx_graph(a,create_using=nx.DiGraph())
See Also¶
nx_pygraphviz, nx_pydot
to_numpy_matrix(G[, nodelist, dtype, order, ...]) | Return the graph adjacency matrix as a NumPy matrix. |
to_numpy_recarray(G[, nodelist, dtype, order]) | Return the graph adjacency matrix as a NumPy recarray. |
from_numpy_matrix(A[, create_using]) | Return a graph from numpy matrix. |
Scipy¶
to_scipy_sparse_matrix(G[, nodelist, dtype, ...]) | Return the graph adjacency matrix as a SciPy sparse matrix. |
from_scipy_sparse_matrix(A[, create_using, ...]) | Return a graph from scipy sparse matrix adjacency list. |