2

Is there a way to use NetworkX to refine the visual properties of a GEXF file after processing it with Gephi? That is, I would like to use Gephi to calculate different network metrics and use them to set the visual properties of network nodes first and then use NetworkX to add some complementary visual properties. An important example of the latter is the use of a specific color palette for nodes according to their cluster membership.

Let's create an example network:

import networkx as nx
G = nx.karate_club_graph()
G.nodes(data=True)

We now have this type of data for each node:

{0: {'club': 'Mr. Hi'} 

Next, let's lay out the network and define the visual node properties in Gephi and then re-read the data:

Here is an example of the GEXF file exported from Gephi:

      <node id="0" label="0">
        <attvalues>
          <attvalue for="0" value="Mr. Hi"></attvalue>
          <attvalue for="weighted degree" value="16.0"></attvalue>
          <attvalue for="modularity_class" value="0"></attvalue>
        </attvalues>
        <viz:size value="37.5625"></viz:size>
        <viz:position x="-93.93568" y="73.28415"></viz:position>
        <viz:color r="104" g="173" b="54"></viz:color>
      </node>

Now, let's read the network and see what we have:

G_layout = nx.read_gexf('karate_club_with_visuals.gexf')
G_layout.nodes(data=True)

Do note that you have to (manually) change the XML root node for the GEXF file to be able to read it with NetworkX.

{'0': {'club': 'Mr. Hi', 'Weighted Degree': 16.0, 'Modularity Class': 0, 'label': '0'},

The metrics produced with Gephi are available in the NetworkX data structure. However, the visual information is missing.

Nevertheless, we are able to define a color for a particular node:

G_layout.nodes['0']['viz'] = {'color': {'r': 255, 'g': 0, 'b': 0, 'a': 0}}

When opening the network again in Gephi we will notice one red node with all the previously defined visuals missing.

To conclude, is there a way to preserve the visual information when post-processing network data in GEXF with NetworkX?

  • 1
    Why don't you use graphml? It preserves all properties, including colors and coordinates. – DYZ May 01 '22 at 18:07
  • Awesome, thanks for the tip @DYZ! This indeed seems to largely solve the problem. I am using GEXF as it is the native format to Gephi. In the future, I will definitely consider using GraphML by default. – Jukka Huhtamäki Jun 04 '22 at 08:54

0 Answers0