<p><strong>更新(3/2019)</strong>:<a href="https://networkx.github.io/documentation/networkx-2.1/_modules/networkx/drawing/nx_pylab.html#draw_networkx" rel="nofollow noreferrer">as of networkx 2.1, the kwargs are forwarded</a>来自<code>draw()</code>,因此您应该能够简单地用<code>edge_color</code>kwarg调用<code>draw()</code>。</p>
<p>好吧,这有点老套,但很管用。这是我想到的。</p>
<p><strong>问题</strong></p>
<p><code>networkx.draw()</code>调用<code>networkx.draw_networkx_nodes()</code>,然后调用<code>pyplot.scatter()</code>来绘制节点。问题是,<code>draw_networkx_nodes()</code>接受的关键字参数没有传递给<code>scatter()</code>。(<a href="http://networkx.github.io/documentation/latest/_modules/networkx/drawing/nx_pylab.html#draw_networkx_nodes" rel="nofollow noreferrer">source here</a>)</p>
<hr/>
<p>为了解决这个问题,我基本上把<code>networkx.draw()</code>分解成它的组成部分:<code>draw_networkx_nodes</code>,<code>draw_networkx_edges</code>,和<code>draw_networkx_labels</code>。</p>
<p><strong>解决方案</strong></p>
<p>我们可以获取<code>draw_networkx_nodes()</code>——a<code>PathCollection</code>的返回值并对其进行操作:您可以分别对颜色或列表使用<code>PathCollection.set_edgecolor()</code>或<code>PathCollection.set_edgecolors()</code>。</p>
<p>示例代码:</p>
<pre><code>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()
</code></pre>
<p>如果您要经常使用它,那么(IMO)重新定义<code>draw_networkx_nodes</code>以实际将kwargs传递给<code>scatter</code>可能更有意义。但上面的<em>会</em>起作用。</p>
<p>要完全删除标记边缘,只需将颜色设置为<code>None</code>,而不是<code>'r'</code>。</p>