python-networkx-使用两个列表绘制不同颜色的节点

2024-04-24 08:49:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚接触networkx,需要帮助。我已经搜索过了,无法解决我的问题。我制作了一个networkx graphviz图像,使用列表作为节点的输入,使用两列文件作为边。第二个文件包含第一个列表中的项,以及对应于节点大小的值。我有另一个文件,其中包含原始列表中的项,我需要这些相同的项以另一种颜色显示,而不更改图形的布局或结构。

下面是我测试过的一些代码:

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()

如果混合列表中的项存在于节点列表中,则我需要能够更改节点的颜色,而无需更改节点列表的结构以保持原始图像结构。我尝试删除与节点中的混合项匹配的项,并使用这两个列表创建不同颜色的节点,但是没有颜色更改,并且图形布局发生了显著更改。我想继续使用graphviz中的“fdp”,除非有人能建议一种将集群从最大到最小垂直放置的方法。

我在搜索中偶然发现了A=nx.to_agraph(G),我确实喜欢这个表示法,而且颜色也按原意改变了,但是图像质量很低,对于较大的簇,什么也看不出来。有人能建议如何提高图像质量吗?或许,让它更大,以伸展出大的集群?

这是原始graphviz fdp图:

enter image description here

这里是A=nx.to_图的输出:

enter image description here

最好同时纠正这两种方法,并感谢所有帮助。


Tags: innode列表forif节点颜色as
3条回答

谢谢你的回复。我一直在寻找的答案似乎就在眼前。我需要制作一个颜色列表,传递给nx.draw_graphviz。

所以,正确的代码(我找到的)将某个颜色传递给比较两个列表的节点:

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)

对于更改文本版本,为了镜像颜色节点版本,我所要做的就是将A.layout()更改为A.layout(prog=“fdp”)

它不会改变布局!

原始图像:

enter image description here

新形象: enter image description here

新文本版本:

这是我用来给图上色的东西。

## 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()

然后我得到了下面的图片。我用的颜色比例子中的多。这就是你想要的吗?

还有,this page有很多例子,我在绘制图表时发现它们很有用。

enter image description here

好吧,我差不多拿到了。我可以改变我想要的节点的颜色,但是它没有保持图形的相同形状,我还可以更新agraph来表示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()

然而,使用fdp格式和agraph使所有内容变黑。如果有人能帮忙的话,我仍然想使节点具有特定的颜色。我还想保留图形的原始形状和格式,只要更改节点颜色,如果还有人可以帮忙的话。我会继续研究这个问题,如果我想出来的话,我会发布另一个答案。感谢所有看过这篇文章的人。(我无法发布更新后的图像,因为图像太大)

相关问题 更多 >