Use NetworkX to create graphs and use Matplotlib or Gephi to show it

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:
graph
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:
Large Graph

References

[1] Overview of NetworkX. (2018, September 19). Retrieved from https://networkx.github.io/documentation/stable/index.html

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xovee

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值