Abstract
In this article I will simply introduce:
- NetworkX, use it to create a graph
- Matplotlib & Gephi, show the graph we created
The environment I use:
- Windows 10
- Python 3.6
- networkx 2.2 (Python Package)
- matplotlib 3.0.1 (Python Package)
- Gephi 0.9.2 (Under Java 11.0.1)
Create a Graph
First, we download and install python package networkx, then import it:
import networkx as nx
next, we create a graph with some nodes and edges:
G = nx.Graph()
G.add_node('a')
G.add_node('b') # add node one by one
G.add_nodes_from([1, 2, 3, ]) # use list to add nodes
G.add_edge('a', 'b') # add edge one by one
G.add_edges_from([('a', 1), ('a', 2), ('a', 3), ]) # use list to add edges
now we have a graph G with four nodes and four edges. We could create a directed graph use nx.DiGraph()
the same.
Show it with Matplotlib
Now we can show this graph, first import python package matplotlib as followings:
import matplotlib.pyplot as plt
then we draw it and show it:
nx.draw(G, with_labels=True)
plt.show()
now check what we got:
LoL, marvelous, isn’t it?
Save the graph
We could save the graph we created into a file, we got many standard graph formats to choose, like GEXF, GraphML, Pickle etc. You can click here to see more formats of graph.
Now we have a python object or graph G, then save or read it using a pickle file:
nx.write_gpickle(G, 'the_great_graph.pkl')
F = nx.read_gpickle('the_great_graph.pkl')
NetworkX is a powerful tool to manipulate complex graphs, plenty of functions included, see the documentation to learn more what you need.
Show it with Gephi
What is Gephi?
Gephi is the leading visualization and exploration software for all kinds of graphs and networks. Gephi is open-source and free.
Runs on Windows, Mac OS X, and Linux.
Sometimes we got a large graph maybe containing more than thousands nodes and edges then hard to show it with matplotlib, but we got another option to visualize it: Gephi.
Download & Install Gephi
Go to the official site to download it, the installation is very easy, just click next, next and next.
Download & Install Java
Seems Gephi need Java 1.8 or later to open it, go to Jave official site download & install it, same easy, but remember the installation path, like C:\Program Files\Java\jdk-11.0.1
.
next, we go to the Gephi installation directory, then in the etc directory, we could see the file gephi.conf, open it with an Editor like notepad.
Find a line starts with # jdkhome="..."'
, then move the character #, substitute the path in quotes with Java installation path, like this:
jdkhome="C:\Program Files\Java\jdk-11.0.1"
Finally, you chould save the gephi.conf file, then open it in Gephi.
Deal with large graphs
In Gephi, we can open a very large graph, and employ many Layout Algorithms to show the graph, like ForceAtlas 2.
Following is an example, this directed graph have 911,714 nodes and 1,216,998 edges:
References
[1] Overview of NetworkX. (2018, September 19). Retrieved from https://networkx.github.io/documentation/stable/index.html