Python - networkx - displays various colored nodes using two lists

I am new to the network and need some help. I searched earlier and could not solve the problem. I have a gridvis networkx image that I made using a list as input for nodes, and a file with two columns for the edges. The second file contains elements from the first list, as well as values ​​corresponding to the size of the node. I have another file that contains the elements that are in the original list, and I need these identical elements so that they appear in a different color, without changing the layout or structure of the chart.

Here is the code I tested:

import sys
from collections import defaultdict
import networkx as nx
import matplotlib.pyplot as plt

inp = sys.argv[1]
cluster = sys.argv[1] + ".cluster"
counts = sys.argv[1] + ".counts"
hybrids = sys.argv[2]

with open(cluster, "r") as f1:
        edges = [line.strip().split('\t') for line in f1]

with open(counts, "r") as f2:
        countsdic = defaultdict(list)
        for line in f2:
                k,v = line.strip().split()
                countsdic[k].append(v)

with open(hybrids, "r") as f3:
        hybrids = [line.strip() for line in f3]

tmp = []

for el in sum(edges, []):
        tmp.append(el)

nodes = []

for t in tmp:
        if t not in nodes:
                nodes.append(t)

node_sizes = {}
for n in nodes:
        node_sizes[n] = ' '.join(countsdic[n])

sizes = []

for v in node_sizes.values():
        x = int(v) * 10
        sizes.append(x)

g = nx.Graph()

g.add_nodes_from(nodes)

g.add_edges_from(edges)

for node in nodes:
        if node in hybrids:
                color = 'green'
        if node not in hybrids:
                color = 'blue'

nx.draw_graphviz(g, prog="fdp", node_color-color, node_size = sizes)

for node in nodes:
        if node in hybrids:
                g.add_node(node, fillcolor='green')
        if node not in hybrids:
                g.add_node(node, fillcolor='blue')

A = nx.to_agraph(g)
A.layout()
A.draw(inp + ".png")

plt.figure(1,figsize=(2000,2000))
plt.savefig(out + ".png", dpi = 1000)
plt.show()

node, , . , , , , . "fdp" , - .

A = nx.to_agraph (G) , , , , , . - , ? , , ?

fdp graphviz:

enter image description here

A = nx.to_graph:

enter image description here

, .

+4
3

, . , , , . nx.draw_graphviz.

, ( ) node, :

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)

, node, , , A.layout() A.layout(prog = "fdp" )

!

:

enter image description here

: enter image description here

:

+3

.

## assign a node attribute, which I am going to color according to
for node in G.nodes():
    G.node[node]['category'] = my_category_dict[node]
## put together a color map, one color for a category
color_map = {'type_A':'b', 'type_B':'#FF0099', 'type_C':'#660066'} 
## construct a list of colors then pass to node_color
nx.draw(G, node_color=[color_map[G.node[node]['category']] for node in G])
plt.show()

, . , . , ?

, , .

enter image description here

+9

, . , , , , graphviz fdp. - , :

with open(counts, "r") as f2:
        countsdic = defaultdict(list)
        for line in f2:
                k,v = line.strip().split()
                countsdic[k].append(v)

with open(hybrids, "r") as f3:
        hybrids = [line.strip() for line in f3]

print hybrids
tmp = []

for el in sum(edges, []):
        tmp.append(el)

nodes = []

for t in tmp:
        if t not in nodes:
                nodes.append(t)

node_sizes = {}
for n in nodes:
        node_sizes[n] = ' '.join(countsdic[n])

sizes = []

for v in node_sizes.values():
        x = int(v) * 10
        sizes.append(x)

g = nx.Graph()
#g.add_nodes_from(nodes)

g.add_edges_from(edges)

#for node in nodes:
#        if node in hybrids:
#                color = 'green'
#        if node not in hybrids:
#                color = 'blue'

pos=nx.graphviz_layout(g, prog='fdp')
nx.draw_networkx_nodes(g, pos, nodelist=[str(n) for n in nodes], node_color='b', node_size = sizes)
nx.draw_networkx_nodes(g, pos, nodelist=[str(n) for n in nodes if n in hybrids], node_color='g', node_size = sizes)
nx.draw_networkx_edges(g,pos)
#nxgraph(graph)

#for node in nodes:
#       if node in hybrids:
#               y.add_node(node, fillcolor='green')
#       if node not in hybrids:
#               g.add_node(node, fillcolor='blue')

A = nx.to_agraph(g)
A.layout(prog="fdp")
A.draw(inp + "2.png")

plt.figure(1,figsize=(2000,2000))
plt.savefig(out + ".png", dpi = 1000)
plt.show()

However, using the fdp format with agraph turned everything black. I would still like to make the nodes specific colors if someone can help with this. I would also like to keep the original shape and format of the graph and just change the color of the node if anyone else can help with this. I will continue to work on this and send another answer if I find out. Thanks to everyone who watched this post. (I could not post an updated image that was too large)

0
source

Source: https://habr.com/ru/post/1547817/


All Articles