When I use python networkx graphviz_layout to draw hierarchical layouts, the corresponding edge disappears in the generated graph when I try to change the weight of the edge.
Below is the specific code and the image:、
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
from networkx.drawing.nx_agraph import graphviz_layout
if __name__ == '__main__':
edges_weight = np.array([
[1, 2, 30], [2, 3, 22], [3, 4, 5.97],
[4, 5, 5.97], [3, 6, 16.03], [6, 7, 4.03],
[7, 8, 4.03], [5, 8, 5.97], [8, 9, 9],
[9, 16, 10], [2, 10, 8], [10, 11, 8],
[11, 12, 2], [12, 13, 2], [11, 13, 10], [13, 15, 8],
[15, 16, 20], [6, 17, 12], [17, 18, 12],
[18, 15, 12], [16, 14, 30]])
G = nx.DiGraph()
for u, v, w in edges_weight:
G.add_edge(u, v, weight=w)
edge_labels = nx.get_edge_attributes(G, 'weight')
print(edge_labels)
shell_pos = graphviz_layout(G, prog='dot')
print(nx.edges(G))
print(shell_pos.items())
print(shell_pos.values())
plt.figure(figsize=(10, 15))
nx.draw_networkx_nodes(G, shell_pos, node_color='lightblue', node_size=1200)
nx.draw_networkx_labels(G, shell_pos)
nx.draw_networkx_edges(G, shell_pos, node_size=1200, arrowstyle='->', arrowsize=10, width=2)
nx.draw_networkx_edge_labels(G, shell_pos, edge_labels=edge_labels)
plt.show()
The data is now [11, 13, 10] , and the image is displayed normally at this time
If I change part of the data → [11, 13, 9], the corresponding frame disappears. I don’t know what has happened. Can anyone help me? Thank you very much!
I have searched for various information but have not yet found any valid results
I expect to be able to display the edges 11->13 normally
I suspect it is an issue where the edges are overlapping with each other and going behind the nodes instead. An alternative suggestion is to manually plot the nodes hierarchically using
NetworkX'smultipartite_layout()rather thangraphviz_layout(). It still has overlapping issues but they are easier to resolve by moving the node orders and subset groupings around.For Example: Multipartite Layout Version of the Graph