我正在使用 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
Run Code Online (Sandbox Code Playgroud)
这不是我对颜色的确切要求,因为不同的节点被分配了不同的颜色,但这是基本思想。