以下代码在图形中为边缘权重生成了非常“狡猾”的标签放置。请看图片。我希望有一个更好的放置位置(靠近每条线的中点),同时仍然可以利用节点的自动定位功能-也就是说,我不想手动定位节点。
有什么想法吗?此外,还有一个警告-<xs:element type="xs:long" name="ItemNo">
<xs:annotation>
<xs:documentation>Item number (table.column))</xs:documentation>
</xs:annotation>
</xs:element>
,如果有人知道怎么办,这将是一个很好的解决方法。
The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.
答案 0 :(得分:2)
来自documentation of draw_networkx:
draw_networkx(G, pos=None, arrows=True, with_labels=True, **kwds) Parameters: [...] pos (dictionary, optional) – A dictionary with nodes as keys and positions as values. If not specified a spring layout positioning will be computed. See networkx.layout for functions that compute node positions.
因此,如果您未显式传递pos
,则会生成spring_layout,但这与您生成的布局不同
pos = nx.spring_layout(G)
,因为两次调用nx.spring_layout(G)会得出不同的结果:
for a in [0,1]:
pos = nx.spring_layout(G)
print(pos)
输出:
{'A': array([ 0.65679786, -0.91414348]), 'B': array([0.34320214, 0.5814527 ]), 'C': array([-1. , 0.33269078])}
{'A': array([-0.85295569, -0.70179415]), 'B': array([ 0.58849111, -0.29820585]), 'C': array([0.26446458, 1. ])}
因此,将相同的pos
传递给两个绘图函数可以解决此问题:
pos = nx.spring_layout(G)
weights = nx.get_edge_attributes(G, "weight")
nx.draw_networkx(G, pos, with_labels=True)
nx.draw_networkx_edge_labels(G, pos, edge_labels=weights)