1
votes

I'm new to Python and I wanted to draw a incidence graph, but with changed labels. I want them to start labeling nodes from '1', not from '0'. Also I want to label edges like this: if It's between node '1' and '2', label It as '12' or '1:2'. Thanks in advance :)

im = np.array([[1, 1, 0, 0, 0, 0], [1, 0, 1, 1, 0, 0], [0, 1, 1, 0, 1, 0], [0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 0, 1]])
am = (np.dot(im, im.T) > 0).astype(int)
np.fill_diagonal(am, 0)
K = nx.from_numpy_matrix(am)

pos = nx.spring_layout(K)
print(pos)

x=1
nx.relabel_nodes(K, lambda x: x + + 1)
nx.draw(K, pos, with_labels=True)

edge_labels = dict(((u, v), d) for u, v, d in K.edges(data=True))
nx.draw_networkx_edge_labels(K, pos, edge_labels = edge_labels, label_pos=0.5)

plt.show()

Example of desired edge labels

1

1 Answers

0
votes

Then build the label you want, just as you've done so far. I'll break this down to basic opeartions; you can build it up again as you wish:

edge_labels = dict(((u, v), 
                    str(u+1) +":"+ str(v+1) +" "+ str(d))
                    for u, v, d in K.edges(data=True))