Book Image

IPython Interactive Computing and Visualization Cookbook

By : Cyrille Rossant
Book Image

IPython Interactive Computing and Visualization Cookbook

By: Cyrille Rossant

Overview of this book

Table of Contents (22 chapters)
IPython Interactive Computing and Visualization Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Manipulating and visualizing graphs with NetworkX


In this recipe, we will show how to create, manipulate, and visualize graphs with NetworkX.

Getting ready

You can find the installation instructions for NetworkX in the official documentation at http://networkx.github.io/documentation/latest/install.html.

With Anaconda, you can type conda install networkx in a terminal. Alternatively, you can type pip install networkx. On Windows, you can also use Chris Gohlke's installer, available at www.lfd.uci.edu/~gohlke/pythonlibs/#networkx.

How to do it…

  1. Let's import NumPy, NetworkX, and matplotlib:

    In [1]: import numpy as np
            import networkx as nx
            import matplotlib.pyplot as plt
            %matplotlib inline
  2. There are many different ways of creating a graph. Here, we create a list of edges (pairs of node indices):

    In [2]: n = 10  # Number of nodes in the graph.
            # Each node is connected to the two next nodes,
            # in a circular fashion.
            adj = [(i, (i+1)%n) for i in range(n...