Colors 向NetworkX上的节点添加颜色属性以导出到Gephi

Colors 向NetworkX上的节点添加颜色属性以导出到Gephi,colors,attributes,visualization,networkx,gephi,Colors,Attributes,Visualization,Networkx,Gephi,我正在使用NetworkX制作一个图形,用Gephi导出可视化。我一直在向图中的节点添加各种属性,没有问题,直到我尝试添加颜色。有人知道如何使用networkx导出带有“彩色”节点的图形吗?(我一直在写gexf文件,但只要它与Gephi兼容,就不在乎它是否是另一种格式。) 下面是我制作图表的代码: def construct_weighted_graph(nodes, distance_dict, weighting_function, color = True): G = nx.Grap

我正在使用NetworkX制作一个图形,用Gephi导出可视化。我一直在向图中的节点添加各种属性,没有问题,直到我尝试添加颜色。有人知道如何使用networkx导出带有“彩色”节点的图形吗?(我一直在写gexf文件,但只要它与Gephi兼容,就不在乎它是否是另一种格式。)

下面是我制作图表的代码:

def construct_weighted_graph(nodes, distance_dict, weighting_function, color = True):

  G = nx.Graph()
  #nodes automatically added when edges added. 
  for sequence in nodes: #loop through and add size attribute for num of sequences
    G.add_node(sequence)
    G.node[sequence]['size'] = distance_dict[sequence][1] #size represented by the node
    if color:
        G.node[sequence]['color'] = (0,1,0)
  for i_node1 in range(len(nodes)):
    dist_list = distance_dict[nodes[i_node1]][-2] #list of distances
    for i_node2 in range(i_node1+1, len(nodes)):
        G.add_edge(nodes[i_node1], nodes[i_node2], 
                   weight = weighting_function(dist_list[i_node2]))
  return G

这并不完全是我对颜色的理解,因为不同的节点分配了不同的颜色,但这是基本思想。

我遇到了完全相同的问题。NetworkX中设置颜色的常规方法不会导出为GEXF格式。通过阅读NetworkX和GEXF文档的导出代码,我了解了如何正确地导出到GEXF,并反过来导入到Gephi

如果着色遵循GEXF的相同结构,NetworkX将导出着色。必须将数据添加到图形中的节点。因此,您向节点添加了一个代表可视化的键“viz”。将“viz”设置为另一个字典,在其中添加一个键“color”,并依次添加一个字典,其中包含键“r”、“g”、“b”和“a”的值

我制作了一个非常简单的示例来演示解决方案:

import networkx as nx
""" Create a graph with three nodes"""
graph = nx.Graph()
graph.add_node('red')
graph.add_node('green')
graph.add_node('blue')
""" Add color data """
graph.node['red']['viz'] = {'color': {'r': 255, 'g': 0, 'b': 0, 'a': 0}}
graph.node['green']['viz'] = {'color': {'r': 0, 'g': 255, 'b': 0, 'a': 0}}
graph.node['blue']['viz'] = {'color': {'r': 0, 'g': 0, 'b': 255, 'a': 0}}
""" Write to GEXF """
# Use 1.2draft so you do not get a deprecated warning in Gelphi
nx.write_gexf(graph, "file.gexf", version="1.2draft")
这会将带有红色、绿色和蓝色节点的图形导出到GEXF

您必须更改尝试将颜色设置为以下颜色的示例:

if color:
    G.node[sequence]['viz'] = {'color': {'r': 0, 'g': 1, 'b': 0, 'a': 0}} # line changed
for i_node1 in range(len(nodes)):

networkx
转换为GEXF格式(版本1.1draft)对我来说也是个错误

但这对我很有效。我在您需要的部分前后添加了一些额外的代码,因为这意味着(在我粗略地重读几年前编写但仍在本网站上使用的代码时:——因为它仍然有效)您将节点添加到有向图(G)然后必须以不同的方式再次添加它们,确保在gexf文件的
标记中对
RGB颜色
大小
格式进行编码

代码

G.add_nodes_from(nodes) #<--- assigns node names, and also node 'size', used below

for n in G.node:
    n_size = G.node[n]['size']
    G.add_node(n,viz={'color':{'r':"170",'b':"170",'g':"170",'a':"0.7"},'size':n_size})
    #{'position':{'x':x,'y':y}}
G.add_weighted_edges_from(edges)
...
import codecs
f = codecs.open(gexf_filename, "wb", "utf-8") # unicode safe version of file-open
nx.write_gexf(G,f)
f.close()
以下是用于调试目的的两个有用的文件示例:

您可以在此处看到它们联机渲染: (带颜色) 和
(同一个图表,没有颜色,但alpha起作用。

“我一直在向图表中的节点添加各种属性,没有问题,直到我尝试添加颜色为止。”尝试添加颜色时发生了什么?回溯(最近一次调用):文件“./distance.py”,第216行,nx格式。写入_gexf(G,path+所需的_gene_File_格式)+“\u graph\u withcolors.gexf”)文件”,第2行,在write\u gexf文件“/usr/lib/python2.7/dist packages/networkx/utils/decorators.py”中,第263行,在“open\u File result=func(*new\u args,**kwargs)文件“/usr/lib/python2.7/dist packages/networkx/readwrite/gexf.py”中,第77行,在write\u gexf writer.add\u graph(G)文件中”/usr/lib/python2.7/dist packages/networkx/readwrite/gexf.py”,第287行,在add_graphself.add_节点(G,graph_元素)文件“/usr/lib/python2.7/dist packages/networkx/readwrite/gexf.py”文件“/usr/lib/python2.7/dist-packages/readwrite/gexf.py”中,第318行,在add_节点节点数据中,默认值为“/usr/lib/lib/python2.7/dist-packages/net“,第385行,在val的add_属性中,开始,结束于v:TypeError:'int'对象不是ITerableC您能更新您的帖子以提供完整的示例吗?”?我可以复制粘贴以查看相同的错误吗?
<?xml version="1.0" encoding="utf-8"?><gexf xmlns:ns0="http://www.gexf.net/1.1draft/viz" version="1.1" xmlns="http://www.gexf.net/1.1draft" xmlns:viz="http://www.gexf.net/1.1draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">
<graph defaultedgetype="directed" mode="static">
<attributes class="node" mode="static">
  <attribute id="0" title="size" type="integer" />
</attributes>
<nodes>
  <node id="0" label="shop">
    <ns0:color b="100" g="100" r="100" />
    <ns0:size value="17" />
    <attvalues>
      <attvalue for="0" value="17" />
    </attvalues>
  </node>
<?xml version='1.0' encoding='utf-8'?>
<gexf version="1.1" xmlns="http://www.gexf.net/1.1draft" xmlns:viz="http://www.gexf.net/1.1draft/viz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance">

<attributes class="node" mode="static">
  <attribute id="0" title="category" type="string" />
  <attribute id="1" title="color" type="string" />
  <attribute id="2" title="size" type="integer" />
</attributes>
<node id="CarterCenter" label="CarterCenter">
    <attvalues>
      <attvalue for="0" value="open data" />
      <attvalue for="1" value="blue" />
      <attvalue for="2" value="0" />
    </attvalues>
</node>