简体   繁体   中英

Gephi - open networkx with colored nodes

I have a rather large.network (34594 nodes and 3897 edges). I created a Networkx.network in Python and gave the nodes a color based on the label of the node. The label links the node to 5 countries based on the beginning letters. In Python, this all worked out fine, but for visualization purposes I exported the file (.gexf) to open it in Gephi. However, when I open it again, it does not have the colors anymore. There is a plug-in (Give color to nodes), however, it seems that I have to change the color manually, node per node. You can import a spreadsheet, which I initially wanted to do, and use as some type of look-up table. However, this is not possible, and you can only import the nodes from there, but those are not linked to the.network.

Does anybody has any insights on how to solve this problem?

Since you are using networkx , I will propose a very simple script that iterates all nodes and colors every node as blue .

import networkx as nx

# Construct a simple line graph
G = nx.path_graph(4)

for v in nx.nodes(G):
    # Set the viz attribute for each node
    G.nodes[v]["viz"] = {} 
    # Set the color to blue
    G.nodes[v]["viz"]["color"] = {"a":0, "r": 0, "g": 0, "b": 255}
     
# output a sample graph
nx.write_gexf(G, 'test.gexf')

The line G.nodes[v]["viz"] = {} , is required to add the viz dictionary to each node, and then we add the ARGB value of each node. In this example a=0 and RGB=(0,0,255) or, simply speaking, blue.

The script is based on networkx documentation , with some small changes. Adding colors should also be possible with an adaptation of nx.setNodeAttibutes , as shown here . It is just that the GEXF format, needs the viz attribute, and then the the corresponding color .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM