set_node_attributes(G, values, name=None)
.. Warning:: The call order of arguments `values` and `name` switched between v1.x & v2.x.
What the node attribute should be set to. If :None:None:`values`
is not a dictionary, then it is treated as a single attribute value that is then applied to every node in G
. This means that if you provide a mutable object, like a list, updates to that object will be reflected in the node attribute for every node. The attribute name will be :None:None:`name`
.
If :None:None:`values`
is a dict or a dict of dict, it should be keyed by node to either an attribute value or a dict of attribute key/value pairs used to update the node's attributes.
Name of the node attribute to set if values is a scalar.
Sets node attributes from a given value or dictionary of values.
>>> G = nx.path_graph(3) >>> bb = nx.betweenness_centrality(G) >>> isinstance(bb, dict) True >>> nx.set_node_attributes(G, bb, "betweenness") >>> G.nodes[1]["betweenness"] 1.0
>>> G = nx.path_graph(3) >>> labels = [] >>> nx.set_node_attributes(G, labels, "labels") >>> labels.append("foo") >>> G.nodes[0]["labels"] ['foo'] >>> G.nodes[1]["labels"] ['foo'] >>> G.nodes[2]["labels"] ['foo']
>>> G = nx.path_graph(3) >>> attrs = {0: {"attr1": 20, "attr2": "nothing"}, 1: {"attr2": 3}} >>> nx.set_node_attributes(G, attrs) >>> G.nodes[0]["attr1"] 20 >>> G.nodes[0]["attr2"] 'nothing' >>> G.nodes[1]["attr2"] 3 >>> G.nodes[2] {}
See :>>> G = nx.Graph() >>> G.add_node(0) >>> nx.set_node_attributes(G, {0: "red", 1: "blue"}, name="color") >>> G.nodes[0]["color"] 'red' >>> 1 in G.nodes False
The following pages refer to to this document either explicitly or contain code examples using this.
networkx.algorithms.assortativity.mixing.attribute_mixing_matrix
networkx.algorithms.operators.binary.compose
networkx.algorithms.bipartite.basic.color
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