如果我使用 NetworkX 和 Matplotlib 多次生成相同的图表,它会在每一代中随机旋转:
Run 1:
Run 2:
在不更改脚本或输入数据的情况下,每次生成图形时都会随机旋转。是否可以指定方向?
随着图形变得更加密集(上面只是示例,但最终我将拥有数千个节点和边),如果由于图形图片旋转而移动新添加的节点或边,将很难看到新添加的节点或边。
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")
A second less important question is why are the edges/lines from R1 to R2 and R1 to R5 so much longer? Update: I hadn't set the "length" attribute in the JSON data source.
我刚刚发现你可以修复随机生成图的初始状态:
只需为生成器提供种子,这样它将始终产生相同的随机位置集。
random_pos = nx.random_layout(graph, seed=42)
pos = nx.spring_layout(graph, pos=random_pos)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)