Color a particular node in Networkx and Graphviz

孤街醉人 提交于 2019-12-03 03:17:42

Since you are using Graphviz to do the drawing you need to use the attributes that Graphviz understands. See https://graphviz.gitlab.io/_pages/doc/info/attrs.html

import networkx as nx
G = nx.Graph()

G.add_node(1,color='red',style='filled',fillcolor='blue',shape='square')
G.add_node(2,color='blue',style='filled')
G.add_edge(1,2,color='green')
G.node[2]['shape']='circle'
G.node[2]['fillcolor']='red'

A = nx.to_agraph(G)
A.layout()
A.draw('color.png')
print A.to_string()

Gives

strict graph {
    graph [bb="0,0,107.21,46.639"];
    node [label="\N"];
    1    [color=red,
        fillcolor=blue,
        height=0.5,
        pos="18,28.639",
        shape=square,
        style=filled,
        width=0.5];
    2    [color=blue,
        fillcolor=red,
        height=0.5,
        pos="89.21,18",
        shape=circle,
        style=filled,
        width=0.5];
    1 -- 2   [color=green,
        pos="36.338,25.899 47.053,24.298 60.519,22.286 71.18,20.694"];
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!