python - networkx - 使用两个列表图形不同的彩色节点 [英] python - networkx - graph different colored nodes using two lists

查看:6075
本文介绍了python - networkx - 使用两个列表图形不同的彩色节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触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 (group,r)as f1:
edges = [line.strip()。split('\t')for line in f1]

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

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

tmp = []

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

nodes = []

for t in tmp:
如果t不在节点中:
nodes.append(t)

node_sizes = {}
在节点n中:
node_sizes [n] =''.join(countsdic [n])

sizes = []

在node_sizes.values()中的v:
x = int(v)* 10
sizes.append(x)

g = nx.Graph()

g.add_nodes_from(nodes)

g.add_edges_from(edges)

节点中的节点:$ b​​ $ b如果节点在混合中:
color ='green'
如果节点不在混合中:
color ='blue'

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

节点中的节点:
如果节点不在混合中:
g.add_node(node,fillcolor ='green')
如果节点不在混合中:
g.add_node )

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

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

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



我在我的搜索中偶然发现了A = nx.to_agraph(G),我喜欢这种表示,颜色也改变了,是低质量的,对于较大的簇,没有什么是可辨别的。任何人都可以建议如何提高图像的质量?也许,让它更大以拉伸大群集?



以下是原始的graphviz fdp图表:





这里是A = nx.to_graph:





更正两种方法是首选,所有的帮助是赞赏。

解决方案

THanks,sophiad您的回复。似乎我所寻找的答案是从我的鼻子在整个时间。我需要一个列表的颜色传递给nx.draw_graphviz。



所以,正确的代码(我发现)传递一个特定的颜色到节点比较两个列表:

  colors = [] 
for n in nodes:
如果n在混合中:
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)



并不改变布局!



原始图像: / p>

>



新图片:



新文本版本:


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 nodes, and a two column file for the edges. A second file contains the items from the first list, as well values which correspond to node size. I have another file, which contains items that are in the original list, and i need those identical items to appear another color, without changing the layout or structure of the graph.

Here's some of the code I've been testing:

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

I need to be able to change the color of the node if the item in the hybrid lists exists in the nodes lists, without altering the structure of the nodes list to maintain the original image structure. I tried removing items that match hybrids in nodes and use both lists to create nodes of different color, however there was no color change, and the graph layout changed significantly. I would like to continue to use the "fdp" from graphviz, unless someone can suggest a way to place the clusters vertically from largest to smallest.

I stumbled upon the A=nx.to_agraph(G) in my searches and I do like the representation, and the colors changed as they were supposed to, however the image is of low quality and for the larger clusters, nothing is discernible. Can anyone suggest how to increase the quality of the image? Perhaps, make it larger to stretch out the large clusters?

Here is the original graphviz fdp graph:

And here is the output form the A=nx.to_graph:

Correcting both methods is preferred, and all help is appreciated.

解决方案

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:

The new image:

The new text version:

这篇关于python - networkx - 使用两个列表图形不同的彩色节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆