python - networkx - graph different colored nodes using two lists

前端 未结 3 1102
猫巷女王i
猫巷女王i 2020-12-30 16:39

I\'m new to networkx and need some help. I\'ve searched previously and couldn\'t resolve my issue. I have a networkx graphviz image I made, using a list as input for the nod

3条回答
  •  太阳男子
    2020-12-30 17:20

    THanks, sophiad for your reply. It seems the answer I was looking for was right in from of my nose the whole time. I needed to make a list of the colors to pass to nx.draw_graphviz.

    So, the correct code (that I found) to pass a certain color to a node comparing two lists:

    colors=[]
    for n in nodes:
        if n in hybrids:
            colors.append('g')
        else:
            colors.append('b')
    
    nx.draw_graphviz(g, prog="fdp", node_color = colors, node_size = sizes)
    

    And for changed the text version, to mirror the color node version, all I had to do was change A.layout() to A.layout(prog="fdp")

    And it does not change the layout!

    The original image:

    enter image description here

    The new image: enter image description here

    The new text version:

提交回复
热议问题