更新(3/2019): 从networkx 2.1开始,kwargs被转发 https://networkx.github.io/documentation/networkx-2.1/_modules/networkx/drawing/nx_pylab.html#draw_networkx from draw()
,所以你应该能够简单地调用draw()
与edge_color
kwarg.
好吧,这有点hacky,但它确实有效。这就是我的想法。
问题
networkx.draw()
calls networkx.draw_networkx_nodes()
,然后调用pyplot.scatter()
绘制节点。问题是关键字 args 接受draw_networkx_nodes()
没有被传递给scatter()
. (来源在这里 http://networkx.github.io/documentation/latest/_modules/networkx/drawing/nx_pylab.html#draw_networkx_nodes)
为了解决这个问题,我基本上分手了networkx.draw()
进入其组件:draw_networkx_nodes
, draw_networkx_edges
, and draw_networkx_labels
.
解决方案
我们可以取返回值draw_networkx_nodes()
-- a PathCollection
-- 并对其进行操作:您可以使用PathCollection.set_edgecolor()
or PathCollection.set_edgecolors()
分别带有颜色或列表。
示例代码:
from networkx import *
import matplotlib.pyplot as plt
G = Graph()
G.add_node(1)
# Need to specify a layout when calling the draw functions below
# spring_layout is the default layout used within networkx (e.g. by `draw`)
pos = spring_layout(G)
nodes = draw_networkx_nodes(G, pos)
# Set edge color to red
nodes.set_edgecolor('r')
draw_networkx_edges(G, pos)
# Uncomment this if you want your labels
## draw_networkx_labels(G, pos)
plt.show()
如果您要经常使用它,那么重新定义可能更有意义(IMO)draw_networkx_nodes
实际上将 kwargs 传递给scatter
。但以上will work.
要完全删除标记边缘,只需将颜色设置为None
代替'r'
.