I am trying to write python code where I want to be able to change the width of each edge.
This is the code I have so far:
import matplotlib.pyplot as plt
import networkx as nx
# create Graph
g = nx.Graph()
# add nodes to graph
g.add_node(1)
g.add_node(2)
g.add_node(3)
g.add_node(4)
g.add_node(5)
g.add_node(6)
g.add_node(7)
g.add_node(8)
g.add_node(9)
g.add_node(10)
g.add_node(11)
g.add_node(12)
g.add_node(13)
g.add_node(14)
g.add_node(15)
g.add_node(16)
g.add_node(17)
g.add_node(18)
g.add_node(19)
g.add_node(20)
g.add_node(21)
# add edges between nodes
g.add_edge(1, 2,width=1)
g.add_edge(1, 3,width=10)
g.add_edge(2, 3,width=10)
g.add_edge(3, 4,width=10)
g.add_edge(4, 5,width=1)
g.add_edge(5, 6,width=10)
g.add_edge(5, 7,width=10)
g.add_edge(6, 7,width=10)
g.add_edge(5, 9,width=10)
g.add_edge(6, 9,width=10)
g.add_edge(7, 8,width=10)
g.add_edge(6, 10,width=10)
g.add_edge(10, 11,width=10)
g.add_edge(6, 11,width=10)
g.add_edge(11, 12,width=10)
g.add_edge(9, 10,width=10)
g.add_edge(9, 15,width=10)
g.add_edge(10, 13,width=10)
g.add_edge(12, 13,width=10)
g.add_edge(13, 14,width=10)
g.add_edge(14, 16,width=10)
g.add_edge(15, 14,width=10)
g.add_edge(9, 14,width=10)
g.add_edge(16, 17,width=10)
g.add_edge(13, 18,width=10)
g.add_edge(17, 18,width=10)
g.add_edge(17, 20,width=10)
g.add_edge(17, 19,widht=10)
g.add_edge(19, 20,width=10)
g.add_edge(18, 20,width=10)
g.add_edge(19, 21,width=10)
g.add_edge(20, 21,width=10)
widths = list(nx.get_edge_attributes(g,'width').values())
# position of nodes of Graph
pos={
1:(13,78),
2:(24,87),
3:(23,77),
4:(25,68),
5:(27,59),
6:(27,46),
7:(17,49),
8:(5,36),
9:(42,58),
10:(40,36),
11:(35,31),
12:(40,12),
13:(61,30),
14:(70,43),
15:(72,55),
16:(82,40),
17:(83,20),
18:(77,15),
19:(93,14),
20:(88,9),
21:(97,4),
}
# drawing graph and specifying properties(node color, edge color, line width, etc)
nx.draw(g, pos, with_labels=True,node_color="orange", node_size=300, edge_color="black", width=list(widths))
# The graph title and showing it
plt.title('Gabriel Graph (102)')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
The problem is that I have of all edge widths set to 10 except 2 but the output shows three edges with widths of 1.
Here is the link to the output: https://i.sstatic.net/akgwo.png
Can someone help fix this?
As I pointed out in the comment above you have a typo. As a result, the list provided as the width
argument to draw
is one short. IMO, networkx should raise an error in this case but it doesn't. Instead, it uses a default width of 1, which is why your "last" edge (20, 21) is assigned a smaller width than specified.
Typos like this can be avoided if you avoid code repetition. I would construct the graph like this:
import networkx as nx
# create Graph
g = nx.Graph()
g.add_nodes_from(range(1, 22))
g.add_edges_from([(1, 2), (4, 5)], width=1)
g.add_edges_from([
(1 , 3),
(2 , 3),
(3 , 4),
(5 , 6),
(5 , 7),
(5 , 9),
(6 , 7),
(6 , 9),
(7 , 8),
(6 , 10),
(6 , 11),
(9 , 10),
(9 , 14),
(9 , 15),
(10 , 11),
(10 , 13),
(11 , 12),
(12 , 13),
(13 , 14),
(13 , 18),
(14 , 16),
(15 , 14),
(16 , 17),
(17 , 18),
(17 , 20),
(17 , 19),
(18 , 20),
(19 , 20),
(19 , 21),
(20 , 21),
], width=10)