Python Forum

Full Version: Python Networkx: Visualize an edge weight with a bubble/circle
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello

I am trying to plot a network in python using networkx. on the edges there should be circle or bubbles with sizes according to the weight of the edges. Lets say I have the nodes A, B, C, D

the edges an their weigths (w):
A->B, w=1
A->C, w=1
A->D, w=2
B->C, w=3
B->D, w=2
C->D, w=4

and this is what I want to have:
[attachment=1247]

Is there any possibility to do that? Or do you have any suggestion how to do that?

This is my code by now, but i was only able to change the edge width, but not to put circles/bubbles on the edge:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.Graph()
G.add_node('A', pos=(0,5))
G.add_node('B', pos=(3,12))
G.add_node('C', pos=(3,6))
G.add_node('D', pos=(7,-4))
weights = [1, 1, 2, 3, 2, 4]
G.add_edge('A', 'B', weight=weights[0])
G.add_edge('A', 'C', weight=weights[1])
G.add_edge('A', 'D', weight=weights[2])
G.add_edge('B', 'C', weight=weights[3])
G.add_edge('B', 'D', weight=weights[4])
G.add_edge('C', 'D', weight=weights[5])
pos=nx.get_node_attributes(G,'pos')

labels = nx.get_edge_attributes(G, 'weight')
nx.draw(G, pos)
nx.draw_networkx_edges(G, pos, width=weights)
nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)

plt.show()