DEV Community

Dendi Handian
Dendi Handian

Posted on

NetworkX graph drawing using Matplotlib

NetworkX is a python library for network/graph analysis. In the real world, the great example usage for network analysis is social network analysis (SNA), which is used for describing the relationship between peoples. Python is widely used for scientific and analysis purposes because there are so many packages that made for it, such as numpy, pandas, scikit-learn, and for the specific field like network analysis, they have networkx. I will show you how to get started with it and view the created graph using the matplotlib package.

Prerequisites to code along

You have installed python on your machine, and could use the pip to install these packages:

pip install --user networkx
pip install --user matplotlib
Enter fullscreen mode Exit fullscreen mode

or if you follow my previous posts about installing python using pyenv and installing packages using pipenv, you are welcome to use these commands because I prefer this way:

pipenv install networkx
pipenv install matplotlib
Enter fullscreen mode Exit fullscreen mode


Let's create two files within a folder for the simple demonstration project, the files are and miserables.json. The miserables.json file contains graph data which you can get it from here.

- project
  |_ miserables.json
Enter fullscreen mode Exit fullscreen mode


Now, let's code the

Importing the packages

We will be using networkx, matplotlib, and built-in package json since we will deal with a JSON file.

import json
import networkx as nx
import matplotlib.pyplot as plt
Enter fullscreen mode Exit fullscreen mode

Loading data from json file

We use the json package to load the JSON file content and convert it into a dictionary object.

miserables_graph = None

with open('miserables.json') as json_file:
    miserables_graph = json.load(json_file)
Enter fullscreen mode Exit fullscreen mode

Creating a networkx graph

Then we create a networkx graph object or instance and add the nodes and edges from the previously imported dictionary object.

G = nx.Graph()

G.add_nodes_from([(node['id'] for node in miserables_graph['nodes'])])

G.add_edges_from([(edge['source'], edge['target'])
                  for edge in miserables_graph['links']])
Enter fullscreen mode Exit fullscreen mode

Drawing the graph

And finally, show the graph using matplotlib.

Enter fullscreen mode Exit fullscreen mode

You can run the file using python or pipenv run python if you're using pipenv. When you run it, there will be a program instance spawn on your desktop.

You may find out the graph drawing is kind off and ugly πŸ˜† I will find how to prettify it later but have fun exploring it yourself.

Top comments (1)

msmprarthanabataju profile image
Prarthana • Edited

Thankyou for the code.
But when I add convexity in the code, where convexity is string, I got the following error:
File :1, in
NameError: name 'convex' is not defined

This is my code :
G.add_edges_from([(int(edge['source']), int(edge['target']),eval(edge['convexity']))
for edge in miserables_graph['links']])

Please do suggest me to solve my error