Saving a Networkx graph in GEXF format and visualize using Gephi
NetworkX is a Python language software package for the creation, manipulation, and study of the structure, dynamics, and function of complex networks. It is used to study large complex networks represented in form of graphs with nodes and edges. Using networkx we can load and store complex networks. We can generate many types of random and classic networks, analyze network structure, build network models, design new network algorithms and draw networks.
In this article we will discuss how we can save a networkx graph in GEXF format and then visualize it using Gephi.
GEXF stands for Graph Exchange XML Format. Although it has features for supporting visualization of graphs but there are some limitations of visualization methods provided by NetworkX library. Hence, the need of using external tools like Gephi used for graph visualization arises. But we cannot straight away export graphs from python to Gephi we need to convert the graph into a format supported by it. GEXF is one such file format.
Gephi needs to installed first to be used: Gephi
Saving the NetworkX graph in gexf format
To achieve this we will be employing write_gexf() function which as the name suggests saves a networkx graph into gexf format easily.
networkx.write_gexf( G , path )
- G: In this argument NetworkX graph object or simply the graph is sent as parameter.
- path: In this argument a valid path for saving the graph is specified.
- Import module
- Create a networkx graph
- Save this graph in gexf format
A file named geeksforgeeks.gexf will be saved at the specified path, it can be viewed using any text editor which will be shown in XML.
Visualizing using Gephi
The graph created in the above program will be visualized using Gephi.
- Install Gephi
- Import gexf file
After visulaization this is what the graph looks like:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.