Drawing NetworkX graphs is important because it helps summarize information and graph/network structure and properties in a manner which is easy to understand and detailed.
NetworkX is not primarily a graph drawing package but basic drawing with Matplotlib as well as an interface to use the open source Graphviz software package are included. These are part of the 'networkx.drawing' module.
For creating and drawing graphs we will use Jupyter notebook.
To install Jupyter notebook enter the following command in a Terminal or Command Line:
pip3 install notebook
To check whether Jupyter notebook is installed or not use the following command:
pip3 show notebook
To open Jupyter notebook use the following command:
python3 -m notebook
Create a new Python3 notebook as shown in the image below.
In the Jupyter notebook, import the networkx module.
import networkx as nx
Create a sample graph.
G = nx.petersen_graph()
Now, we will draw this graph in different ways:
Using the draw() method.
nx.draw(G)
The output looks like this.
Using the draw() method with labels.
nx.draw(G, with_labels=True, font_weight='bold')
The output looks like this.
Using the draw_shell() method with labels.
nx.draw_shell(G, nlist=[range(5, 10), range(5)],
with_labels=True, font_weight='bold')
The output looks like this.
Comments