Node在python networkx中没有边界

时间:2017-07-02 19:22:18

标签: python networkx

如何使用networkx获取节点的行边界?见下图。我的问题是由于白色节点。无论颜色如何,我都想展示它们的形状。通过与其他节点比较,参见节点4。

代码示例

import networkx as nx
import matplotlib.pyplot as plot

g= {1: [2, 3],
    2: [2, 3, 4],
    3: [1, 2],
    4: [2]}

graph= nx.Graph()

for v in g:
    graph.add_node(v)

for v in g:
    for u in g[v]:
        graph.add_edge(v, u)

nx.draw_networkx(graph, node_color=['r', 'r', 'r', 'w'], node_shape='o')

plot.axis('off')
plot.show()

输出

enter image description here

1 个答案:

答案 0 :(得分:4)

我明白了。

ax= plot.gca()
ax.collections[0].set_edgecolor("#000000")

添加上面的行以设置节点边缘的颜色。

import networkx as nx
import matplotlib.pyplot as plot

g= {1: [2, 3],
    2: [2, 3, 4],
    3: [1, 2],
    4: [2]}

graph= nx.Graph()

for v in g:
    graph.add_node(v)
    for u in g[v]:
        graph.add_edge(v, u)

nx.draw_networkx(graph, node_color=['r', 'r', 'r', 'w'], node_shape='o')

ax= plot.gca()
ax.collections[0].set_edgecolor("#000000")

plot.axis('off')
plot.show()
相关问题