networkx Getting started with networkx

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!


This section provides an overview of what networkx is, and why a developer might want to use it.

It should also mention any large subjects within networkx, and link out to the related topics. Since the Documentation for networkx is new, you may need to create initial versions of those related topics.

Basic program for displaying nodes in matplotlib using networkx

import networkx as nx  # importing networkx package
import matplotlib.pyplot as plt # importing matplotlib package and pyplot is for displaying the graph on canvas 
'''Node can be called by any python-hashable obj like string,number etc'''
nx.draw(b)  #draws the networkx graph containing nodes which are declared till before  # displays the networkx graph on matplotlib canvas

Additional clarification:

nx.draw(b,nodelist=[1,'helloworld']) #displays the particular nodes which are given by nodelist only 
nx.draw_networkx(b,nodelist=[1,'helloworld']) #displays the node along with its name given by us i.e 1, hello  respectively

Installation or Setup

Welcome to the world of Graphs. If you have connected data then you might need one of the types of graphs to model those patterns. There are several things that can be done with Graphs like mapping traffic patterns, managing water distribution networks, social media analysis, etc... At it's heart we need to be able to create Nodes and Edges with data associated about each.

NetworkX is a library dealing with Graph Database creation/import/export/manipulation/algorithms/plotting. You can start using several types of network graphs. For example: Facebook might put their users in a plain Graph()

import networkx as nx
facebook = nx.Graph()  
if both_agree_to_be_friends('you','me'):
    facebook.add_edge('you','me') #order isn't important here.

Facebook would use a regular Graph() because there isn't anything special about the edge between nodes. This Facebook example can only have one edge (friendship) between nodes.

Another type of Graph would be a Directed Graph. Twitter would use a Directed Graph because the nodes have a direction. In Twitter I can follow you but you don't have to follow me. So we could represent that with this code:

import networkx as nx
twitter = nx.DiGraph()
twitter.add_edge('me','you') #order is important here.

This is how Twitter might set up 'me' to follow 'you', but not the other way around.

There are MultiGraphs() and MultiDiGraphs() as well just in case you want more than one edge between two nodes. These four types cover a wide variety of problems that can be represented with Graphs. If you want you can add a dictionary of data to both nodes and edges. The documentation on this module is incredible. Every algorithm is well researched and well implemented. I hope you enjoy working with it.

Installation instructions: The detailed instructions on installing NetworkX is available here.

As with any other python package, NetworkX can be installed using pip, Miniconda/Anaconda and from source code.

Installing with pip

pip install networkx

An attempt will be made to find and install an appropriate version of NetworkX that matches your operating system and Python version.

To use pip, you need to have setuptools installed.

If you want to install the development version from GitHub, use the command

pip install git://

Miniconda and Anaconda use conda for software installation/updates.

NetworkX is currently installed with Anaconda. Miniconda doesn't come with NetworkX by default.

You can update/install NetworkX to the latest version with:

conda install networkx

or if you want to update NetworkX installation then

conda update networkx

Installing from source

Source file archive

  1. Download the source from or get the latest version.
  2. Unpack and change directory to the source directory (it should have the files README.txt and
  3. Run python install to build and install
  4. (Optional) Run nosetests to execute the tests if you have nose installed.

Installing from GitHub

  1. Clone the NetworkX repository (see for options)

    git clone
  2. Change directory to NetworkX

  3. Run python install to build and install

  4. (Optional) Run nosetests to execute the tests if you have nose installed.

If you don’t have permission to install software on your system, you can install into another directory using the --user , --prefix , or --home flags to

Requirements To use NetworkX you need Python 2.7, 3.3 or later

Optional Packages

  1. NumPy: Provides matrix representation of graphs and is used in some graph algorithms for high-performance matrix computations. (
  2. SciPy: Provides sparse matrix representation of graphs and many numerical scientific tools. (
  3. Matplotlib: Provides flexible drawing of graphs. (
  4. GraphViz in conjunction with either PyGraphviz ( or pydotplus ( provides graph drawing and graph layout algorithms. (
  5. PyYAML: Required for YAML format reading and writing. (

3: 2:

Got any networkx Question?