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

查看:803
本文介绍了在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.

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天全站免登陆