1

I have a text file with the following data:

192.168.12.22 192.168.12.21 23
192.168.12.21 192.168.12.22 26
192.168.12.23 192.168.12.22 56

There are three nodes and two of them are sending packets to each other. I want to be able to show both weights on two different edges, but it only shows one with a single weight.

This is my code:

import networkx as nx
import matplotlib.pyplot as plt

G = nx.read_weighted_edgelist('test.txt', create_using=nx.DiGraph())
pos = nx.spring_layout(G)

print(nx.info(G))

nx.draw(G, pos, with_labels=True)    
nx.draw_networkx_edge_labels(G, pos)
plt.show()

Output

machnic
  • 2,304
  • 2
  • 17
  • 21
cbarb
  • 25
  • 3

1 Answers1

1

You can use the label_pos parameter (see draw_networkx_edge_labels):

import networkx as nx
import matplotlib.pyplot as plt

edges = [["192.168.12.22", "192.168.12.21", 23],
         ["192.168.12.21", "192.168.12.22", 26],
         ["192.168.12.23", "192.168.12.22", 56]]

graph = nx.DiGraph()
graph.add_weighted_edges_from(edges)

pos = nx.spring_layout(graph)

nx.draw(graph, pos, with_labels=True)
nx.draw_networkx_edge_labels(graph,
                             pos,
                             edge_labels={(u, v): d for u, v, d in graph.edges(data="weight")},
                             label_pos=.66)
plt.show()

You may also want to take a look at this answer.

Sparky05
  • 4,692
  • 1
  • 10
  • 27