logo

Reading and Writing GEXF Files in NetworkX 📂Graph Theory

Reading and Writing GEXF Files in NetworkX

Explanation1 2

GEXF stands for Graph Exchange XML Format, a language for describing graph structures. Considering the explanation that it started together with the Gephi project, it seems to be designed to be easily handled by Gephi.

Code

Writing

Let’s create the following graph with NetworkX.

import networkx as nx
from itertools import combinations

>>> G = nx.Graph()
>>> IVE = ["가을", "안유진", "레이", "장원영", "리즈", "이서"]
>>> LESSERAFIM = ["사쿠라", "김채원", "허윤진", "카즈하", "홍은채"]
>>> IZONE = ["사쿠라", "김채원", "안유진", "장원영"]
>>>
>>> G.add_nodes_from(IVE, group="IVE")
>>> G.add_nodes_from(LESSERAFIM, group="LESSERAFIM")
>>>
>>> G.add_edges_from(list(combinations(IVE, 2)))
>>> G.add_edges_from(list(combinations(LESSERAFIM, 2)))
>>> G.add_edges_from(list(combinations(IZONE, 2)))

Using write_gexf(), we can save a NetworkX graph to a gexf file.

>>> nx.write_gexf(G,'IZONE_network.gexf')

When opened in Gephi, the visualization looks like this.

izone_network.png

Reading

read_gexf(path) can be used to read a gexf file.

>>> G = nx.read_gexf("IZONE_network.gexf")
>>> G
<networkx.classes.graph.Graph object at 0x0000027F09DDDB20>

Environment

  • OS: Windows11
  • Version: Python 3.9.2, networkx 2.8.6, Gephi 0.9.7