I would like to create a graph where nodes have a different weight and colour based on values in columns.
A sample of data is
Node | Weight | Colour | Neighbours |
---|---|---|---|
1 | 23 | red | [3,5] |
3 | 18 | blue | [2] |
4 | 50 | blue | [] |
5 | 18 | blue | [1] |
2 | 36 | green | [3] |
The table above shows the links by nodes:
- node 1 is linked with 3 and 5. It has weight 23 and colour red
- node 2 is linked with 3. It has weight 36 and colour green
- node 3 is linked with 2. It has weight 18 and colour blue
- node 4 has not links. It has colour blue and weight 50
- node 5 is linked with 1. It has weight 18 and colour blue
For building the network I have done as follows
d = dict(df.drop_duplicates(subset=['Node','Colour'])[['Node','Colour']].to_numpy().tolist())
nodes = G.nodes()
plt.figure(figsize=(30,30))
pos = nx.draw(G, with_labels=True,
nodelist=nodes,
node_color=[d.get(i,'lightgreen') for i in nodes],
node_size=1000)
Unfortunately the colours are all wrong! Also, I have difficulties to add information on weights.
The ties among nodes should have weights assigned to them.
I have tried with edge_attr='weight'
in nx.draw
, where 'weight' = df['Weight']
.
I hope you can give me some help and let me know what I have done wrong.