to_numpy_matrix(G, nodelist=None, dtype=None, order=None, multigraph_weight=<built-in function sum>, weight='weight', nonedge=0.0)
For directed graphs, entry i,j corresponds to an edge from i to j.
The matrix entries are assigned to the weight edge attribute. When an edge does not have a weight attribute, the value of the entry is set to the number 1. For multiple (parallel) edges, the values of the entries are determined by the :None:None:`multigraph_weight`
parameter. The default is to sum the weight attributes for each of the parallel edges.
When :None:None:`nodelist`
does not contain every node in G
, the matrix is built from the subgraph of G
that is induced by the nodes in :None:None:`nodelist`
.
The convention used for self-loop edges in graphs is to assign the diagonal matrix entry value to the weight attribute of the edge (or the number 1 if the edge has no weight attribute). If the alternate convention of doubling the edge weight is desired the resulting Numpy matrix can be modified as follows:
>>> import numpy as np >>> G = nx.Graph([(1, 1)]) >>> A = nx.to_numpy_matrix(G) >>> A matrix([[1.]]) >>> A[np.diag_indices_from(A)] *= 2 >>> A matrix([[2.]])
The NetworkX graph used to construct the NumPy matrix.
The rows and columns are ordered according to the nodes in :None:None:`nodelist`
. If :None:None:`nodelist`
is None, then the ordering is produced by G.nodes().
A valid single NumPy data type used to initialize the array. This must be a simple type such as int or numpy.float64 and not a compound data type (see to_numpy_recarray) If None, then the NumPy default is used.
Whether to store multidimensional data in C- or Fortran-contiguous (row- or column-wise) order in memory. If None, then the NumPy default is used.
An operator that determines how weights in multigraphs are handled. The default is to sum the weights of the multiple edges.
The edge attribute that holds the numerical value used for the edge weight. If an edge does not have that attribute, then the value 1 is used instead.
The matrix values corresponding to nonedges are typically set to zero. However, this could be undesirable if there are matrix values corresponding to actual edges that also have the value zero. If so, one might prefer nonedges to have some other value, such as nan.
Graph adjacency matrix
Returns the graph adjacency matrix as a NumPy matrix.
>>> G = nx.MultiDiGraph()
... G.add_edge(0, 1, weight=2) 0
>>> G.add_edge(1, 0) 0
>>> G.add_edge(2, 2, weight=3) 0
>>> G.add_edge(2, 2) 1
>>> nx.to_numpy_matrix(G, nodelist=[0, 1, 2]) matrix([[0., 2., 0.], [1., 0., 0.], [0., 0., 4.]])See :
The following pages refer to to this document either explicitly or contain code examples using this.
networkx.convert_matrix.to_numpy_matrix
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