为 NetworkX 上的节点添加颜色属性以导出到 Gephi [英] Adding Color Attribute to Nodes on NetworkX to export to Gephi

查看:34
本文介绍了为 NetworkX 上的节点添加颜色属性以导出到 Gephi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 NetworkX 制作图表以导出以使用 Gephi 进行可视化.我一直在向图表中的节点添加各种属性,没有问题,直到我尝试添加颜色.有谁知道如何使用networkx导出带有彩色"节点的图形?(我一直在写一个gexf文件,不过不管是不是别的格式,只要和Gephi兼容就行.)

I am making a graph using NetworkX to export to visualize with Gephi. I've been adding various attributes to the nodes in my graph, without issue, until I tried adding colors. Does anyone know how to export a graph with "colored" nodes using networkx? (I've been writing into a gexf file, but don't care if it is another format as long as it is compatible with Gephi.)

这是我制作图表的代码:

Here is the code in which I make the graph:

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

这不完全是我对颜色的看法,因为不同的节点被分配了不同的颜色,但这是基本思想.

That is not exactly what I have for color, since different nodes are assigned different colors, but it is the basic idea.

推荐答案

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

I ran into the exactly the same issue. The normal way to set coloring in NetworkX does not get exported to the GEXF format. By reading the export code of NetworkX and GEXF documentation, I have found out how to correctly export to GEXF and in turn import into Gephi.

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

NetworkX does export coloring if the coloring follows the same structure of GEXF. The data has to be added to a node inside a graph. So you add a key 'viz' standing for visualization to the node. You set 'viz' to another dictionary, where you add a key 'color' with in turn a dictionary with values for keys 'r','g','b', and 'a'.

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

I made a very simple example that demonstrates the solution:

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.

This exports a graph with a red, a green, and a blue node to GEXF.

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

You have to change your example where you try to set the color to:

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 上的节点添加颜色属性以导出到 Gephi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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