Bravo
Bravo

Reputation: 677

How to draw node and edge attributes in a networkx graph

I have a graph G with attribute 'state' for nodes and edges. I want to draw the graph, all nodes labelled, and with the state marked outside the corresponding edge/node.

for v in G.nodes():     
    G.node[v]['state']='X'
G.node[1]['state']='Y' 
G.node[2]['state']='Y'

for n in G.edges_iter():    
    G.edge[n[0]][n[1]]['state']='X'
G.edge[2][3]['state']='Y'

The command draw.networkx has an option for labels, but I do not understand how to provide the attribute as a label to this command. Could someone help me out?

Upvotes: 32

Views: 41281

Answers (2)

Aric
Aric

Reputation: 25319

It's not so pretty - but it works like this:

from matplotlib import pyplot as plt
import networkx as nx
G = nx.Graph()
G.add_edge(1,2)
G.add_edge(2,3)
for v in G.nodes():
    G.node[v]['state']='X'
G.node[1]['state']='Y'
G.node[2]['state']='Y'

for n in G.edges_iter():
    G.edge[n[0]][n[1]]['state']='X'
G.edge[2][3]['state']='Y'

pos = nx.spring_layout(G)

nx.draw(G, pos)
node_labels = nx.get_node_attributes(G,'state')
nx.draw_networkx_labels(G, pos, labels = node_labels)
edge_labels = nx.get_edge_attributes(G,'state')
nx.draw_networkx_edge_labels(G, pos, edge_labels = edge_labels)
plt.savefig('this.png')
plt.show()

enter image description here

Upvotes: 44

cottontail
cottontail

Reputation: 23381

@Aric's solution is outdated and doesn't run anymore. The following code produces a similar graph. The only difference is that the plot below draws the node attributes separately from node labels.

import networkx as nx
# define a graph
G = nx.Graph()
edges = [(1, 2), (2, 3)]
G.add_edges_from(edges)

# set node attributes
for n in G.nodes:
    val = 'X' if n == 3 else 'Y'
    nx.set_node_attributes(G, {n: {'state': val}})
    
# set edge attributes
for e in G.edges:
    val = 'X' if e == (1, 2) else 'Y'
    nx.set_edge_attributes(G, {e: {'state': val}})

pos = nx.spring_layout(G, seed=0)

# get edge and node attributes
edge_labels = nx.get_edge_attributes(G, 'state')
node_states = nx.get_node_attributes(G, 'state')
# set node state positions
state_pos = {n: (x+0.12, y+0.05) for n, (x,y) in pos.items()}

# draw graph
nx.draw_networkx(G, pos, node_size=600)
# draw node state labels
nx.draw_networkx_labels(G, state_pos, labels=node_states, font_color='red')
# draw edge attributes
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels);

result

Upvotes: 4

Related Questions