如果我使用NetworkX和Matplotlib多次生成同一图形,则每次生成时都会随机旋转:
第二个不太重要的问题是为什么从R1到R2和从R1到R5的边/线如此之长?更新:我没有在JSON数据源中设置"length"属性。
在不更改脚本或输入数据的情况下,每次生成图形时都会随机旋转。是否可能指定方向?
随着图形变得越来越密集(上面仅是样本,但最终我将拥有数千个节点和边),如果移动了图形图像,新添加的节点或边将很难看到。
import networkx as nx
import matplotlib.pyplot as plt
from networkx.readwrite import json_graph
#
# The graph data is loaded from JSON
#
graph = json_graph.node_link_graph(input_json)
pos = nx.spring_layout(graph)
nx.draw(graph, pos, with_labels=True, node_size=300)
edge_labels=dict([((u,v,),d['weight']) for u,v,d in graph.edges(data=True)])
nx.draw_networkx_edge_labels(graph, pos, edge_labels=edge_labels)
plt.savefig("test.png")
fixed = ["R1", "R2", "R3", "R4", "R5"]; nx.spring_layout(graph, pos=positions_json, fixed=fixed)
- jwbensley