Storing and Accessing node attributes python networkx

As you say, it’s just a matter of adding the attributes when adding the nodes to the graph G.add_node(‘abc’, dob=1185, pob=’usa’, dayob=’monday’) or as a dictionary G.add_node(‘abc’, {‘dob’: 1185, ‘pob’: ‘usa’, ‘dayob’: ‘monday’}) To access the attributes, just access them as you would with any dictionary G.node[‘abc’][‘dob’] # 1185 G.node[‘abc’][‘pob’] # usa G.node[‘abc’][‘dayob’] # monday … Read more

Is there a way to guarantee hierarchical output from NetworkX?

If you use a directed graph then the Graphviz dot layout will do something like you want with the tree. Here is some code similar to the above solutions that shows how to do that import networkx as nx from networkx.drawing.nx_agraph import graphviz_layout import matplotlib.pyplot as plt G = nx.DiGraph() G.add_node(“ROOT”) for i in range(5): … Read more

how to draw directed graphs using networkx in python?

Fully fleshed out example with arrows for only the red edges: import networkx as nx import matplotlib.pyplot as plt G = nx.DiGraph() G.add_edges_from( [(‘A’, ‘B’), (‘A’, ‘C’), (‘D’, ‘B’), (‘E’, ‘C’), (‘E’, ‘F’), (‘B’, ‘H’), (‘B’, ‘G’), (‘B’, ‘F’), (‘C’, ‘G’)]) val_map = {‘A’: 1.0, ‘D’: 0.5714285714285714, ‘H’: 0.0} values = [val_map.get(node, 0.25) for node … Read more