The code below produces a very "dodgy" placement of the labels for edge weights in a graph. Please see image. I would like to have a better placement (close to the midpoint of each line), while still taking advantage of the automatic positioning of the nodes - i.e. I don't want to have to manually position the nodes.
Any ideas please? Also there is a warning - The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
which would be good to address if anyone knows how.
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
G = nx.Graph()
G.add_nodes_from(["A", "B", "C"])
G.add_edge("A", "B", weight=5)
G.add_edge("B", "C", weight=7)
G.add_edge("C", "A", weight=2)
pos = nx.spring_layout(G)
weights = nx.get_edge_attributes(G, "weight")
nx.draw_networkx(G, with_labels=True)
nx.draw_networkx_edge_labels(G, pos, edge_labels=weights)
plt.show()
See Question&Answers more detail:os