Labeling edges in networkx
Question:
I’m programming a basic neural network and want to plot it as a picture. For that, I created all the nodes and edges I need.
for l, j in zip(self.layers, range(len(self.layers))):
for n, i in zip(l.neurons, range(len(l.neurons))):
fixed_positions[n.identifier] = (j, i)
for l in self.layers:
for n in l.neurons:
for c, w in zip(n.inconnections, n.inconnectionweights):
g.add_edge(n.identifier, c.identifier)
fixed_nodes = fixed_positions.keys()
pos = nx.spring_layout(g, pos=fixed_positions, fixed=fixed_nodes)
the blue points (imagine them on all edges) are where I want to add a label onto the edges, but I don’t know how to do it. It’s supposed to work for any reasonable net size, i.e. it should also work for 4, 3 and 2 neurons in the respective layers.
Answers:
Here is an example for ploting edge label in networkx, hope it will help you.
import matplotlib.pyplot as plt
import networkx as nx
edges = [['A', 'B'], ['B', 'C'], ['B', 'D']]
G = nx.Graph()
G.add_edges_from(edges)
pos = nx.spring_layout(G)
plt.figure()
nx.draw(
G, pos, edge_color='black', width=1, linewidths=1,
node_size=500, node_color='pink', alpha=0.9,
labels={node: node for node in G.nodes()}
)
nx.draw_networkx_edge_labels(
G, pos,
edge_labels={('A', 'B'): 'AB',
('B', 'C'): 'BC',
('B', 'D'): 'BD'},
font_color='red'
)
plt.axis('off')
plt.show()
you can use the edge attributes of G
nx.draw(G, with_labels=True, node_color='skyblue', edge_cmap=plt.cm.Blues, pos = pos)
edge_labels = nx.get_edge_attributes(G,'edge') # key is edge, pls check for your case
formatted_edge_labels = {(elem[0],elem[1]):edge_labels[elem] for elem in edge_labels} # use this to modify the tuple keyed dict if it has > 2 elements, else ignore
nx.draw_networkx_edge_labels(G,pos,edge_labels=formatted_edge_labels,font_color='red')
plt.show()
You can use draw_networkx_edge_labels(edge_labels) to draw label between edges.
- If
edge_labels
is not given, the attributes of edge is used.
edge_labels
should be a dictionary keyed by edge two-tuple of text labels. Only labels for the keys in the dictionary are drawn.
To iterate through the edges of graph, you can use G.edges.
G.edges
returns a list of (node1, node2)
, where node1
and node2
are two nodes of the edge.
G.edges(data=True)
returns a list of (node1, node2, ddict)
, where ddict
is edge attribute dict.
G.edges(data=attr)
returns a list of (node1, node2, ddict[attr])
import matplotlib.pyplot as plt
import networkx as nx
G = nx.DiGraph()
G.add_edges_from([(1, 2), (1, 3), (2, 3)])
pos = nx.spring_layout(G)
nx.draw_networkx(G, pos)
edge_labels = dict([((n1, n2), f'{n1}->{n2}')
for n1, n2 in G.edges])
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
plt.show()
With G.edges(data=True)
import matplotlib.pyplot as plt
import networkx as nx
G = nx.Graph()
G.add_edge(1, 2, weight=3)
G.add_edge(2, 3, weight=5)
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True)
edge_labels = dict([((n1, n2), d['weight'])
for n1, n2, d in G.edges(data=True)])
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, label_pos=0.9,
font_color='red', font_size=16, font_weight='bold')
plt.show()
I’m programming a basic neural network and want to plot it as a picture. For that, I created all the nodes and edges I need.
for l, j in zip(self.layers, range(len(self.layers))):
for n, i in zip(l.neurons, range(len(l.neurons))):
fixed_positions[n.identifier] = (j, i)
for l in self.layers:
for n in l.neurons:
for c, w in zip(n.inconnections, n.inconnectionweights):
g.add_edge(n.identifier, c.identifier)
fixed_nodes = fixed_positions.keys()
pos = nx.spring_layout(g, pos=fixed_positions, fixed=fixed_nodes)
the blue points (imagine them on all edges) are where I want to add a label onto the edges, but I don’t know how to do it. It’s supposed to work for any reasonable net size, i.e. it should also work for 4, 3 and 2 neurons in the respective layers.
Here is an example for ploting edge label in networkx, hope it will help you.
import matplotlib.pyplot as plt
import networkx as nx
edges = [['A', 'B'], ['B', 'C'], ['B', 'D']]
G = nx.Graph()
G.add_edges_from(edges)
pos = nx.spring_layout(G)
plt.figure()
nx.draw(
G, pos, edge_color='black', width=1, linewidths=1,
node_size=500, node_color='pink', alpha=0.9,
labels={node: node for node in G.nodes()}
)
nx.draw_networkx_edge_labels(
G, pos,
edge_labels={('A', 'B'): 'AB',
('B', 'C'): 'BC',
('B', 'D'): 'BD'},
font_color='red'
)
plt.axis('off')
plt.show()
you can use the edge attributes of G
nx.draw(G, with_labels=True, node_color='skyblue', edge_cmap=plt.cm.Blues, pos = pos)
edge_labels = nx.get_edge_attributes(G,'edge') # key is edge, pls check for your case
formatted_edge_labels = {(elem[0],elem[1]):edge_labels[elem] for elem in edge_labels} # use this to modify the tuple keyed dict if it has > 2 elements, else ignore
nx.draw_networkx_edge_labels(G,pos,edge_labels=formatted_edge_labels,font_color='red')
plt.show()
You can use draw_networkx_edge_labels(edge_labels) to draw label between edges.
- If
edge_labels
is not given, the attributes of edge is used. edge_labels
should be a dictionary keyed by edge two-tuple of text labels. Only labels for the keys in the dictionary are drawn.
To iterate through the edges of graph, you can use G.edges.
G.edges
returns a list of(node1, node2)
, wherenode1
andnode2
are two nodes of the edge.G.edges(data=True)
returns a list of(node1, node2, ddict)
, whereddict
is edge attribute dict.G.edges(data=attr)
returns a list of(node1, node2, ddict[attr])
import matplotlib.pyplot as plt
import networkx as nx
G = nx.DiGraph()
G.add_edges_from([(1, 2), (1, 3), (2, 3)])
pos = nx.spring_layout(G)
nx.draw_networkx(G, pos)
edge_labels = dict([((n1, n2), f'{n1}->{n2}')
for n1, n2 in G.edges])
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
plt.show()
With G.edges(data=True)
import matplotlib.pyplot as plt
import networkx as nx
G = nx.Graph()
G.add_edge(1, 2, weight=3)
G.add_edge(2, 3, weight=5)
pos = nx.spring_layout(G)
nx.draw(G, pos, with_labels=True)
edge_labels = dict([((n1, n2), d['weight'])
for n1, n2, d in G.edges(data=True)])
nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, label_pos=0.9,
font_color='red', font_size=16, font_weight='bold')
plt.show()