Reader small image

You're reading from  Network Science with Python and NetworkX Quick Start Guide

Product typeBook
Published inApr 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789955316
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
Edward L. Platt
Edward L. Platt
author image
Edward L. Platt

Edward L. Platt creates technology for communities and communities for technology. He is currently a researcher at the University of Michigan School of Information and the Center for the Study of Complex Systems. He has published research on large-scale collective action, social networks, and online communities. He was formerly a staff researcher at the MIT Center for Civic Media. He contributes to many free/open source software projects, including tools for media analysis, network science, and cooperative organizations. He has also done research on quantum computing and fault tolerance. He has an M.Math in Applied Mathematics from the University of Waterloo, as well as B.S degrees in both Computer Science and Physics from MIT.
Read more about Edward L. Platt

Right arrow

Your first network in NetworkX

Now, let's create and visualize a small network using NetworkX! For the code in this book, you will need Python 3.4 or higher and NetworkX 2.2 or greater. I also highly recommend Jupyter Lab as an interactive Python environment. The code in this book is available as Jupyter notebooks at https://github.com/PacktPublishing/Network-Science-with-Python-and-NetworkX-Quick-Start-Guide.

The code in this book was written using NetworkX 2.3. At time of writing, NetworkX 2.3 is available but has not been officially released. All of the examples will work with NetworkX 2.2, but may have minor differences in node color and formatting.

The following example creates an undirected, unweighted network, adds edges and nodes, and then generates a visualization. Other types of networks will be discussed in Chapter 2, Working with Networks in NetworkX. First, we import the networkx package. In this book, I will use the convention of importing the library with the alias nx:

import networkx as nx

Next, we create a Graph object, representing an undirected network, given as follows:

G = nx.Graph()

Now that the graph exists, we can add nodes one at a time with the add_node() method, or all at once with add_nodes_from(). When adding nodes to a network, each node has to have a unique ID. The ID can be a number, a string, or a tuple. In fact, you can use any Python object as an ID, as long as it has a __hash__() method defined. For this example, we'll use letters as node IDs, shown as follows:

G.add_node('A')
G.add_nodes_from(['B', 'C'])

Similarly, edges can be added one at a time with add_edge(), or all at once with add_edges_from(), shown as follows:

G.add_edge('A', 'B')
G.add_edges_from([('B', 'C'), ('A', 'C')])

So far, the A, B, and C nodes, as well as the edges connecting them, have been added. The following code draws a simple visualization of the network:

plt.figure(figsize=(7.5, 7.5))
nx.draw_networkx(G)
plt.show()

In the preceding code, figure() is used to create a 7.5 by 7.5 inch figure, which will hold the visualization. The draw_networkx() function uses the Graph object G to produce a visualization. The show() function renders the visualization, but can be omitted if you are running the examples in Jupyter Lab. The visualization should appear similar to the following:

Output of draw_networkx()
NetworkX has several functions for visualizing networks, each of which allows you to customize the visualization style. Some of these functions and parameters are discussed over the remaining chapters, in particular Chapter 11, Visualization.

Your visualization may look slightly different from the one shown previously. This is because visualizations in NetworkX sometimes use randomized algorithms. The randomized algorithms can be configured to produce the same output each time by setting the random seed. The following code sets the random seeds used by NetworkX (this code will also appear at the beginning of the example code for all future chapters):

import random
from numpy import random as nprand
seed = hash("Network Science in Python") % 2**32
nprand.seed(seed)
random.seed(seed)
The pyplot figure() function is used in the preceding code to set the size of the visualization figure, but doing that each time can be tedious. Instead, it is possible to set the default figure size as follows:

plt.rcParams.update({'figure.figsize': (7.5, 7.5)})

Nodes can also be added using a nifty shortcut. If you try to add an edge referring to a node ID that isn't in the network, NetworkX will automatically add the node! So, in practice, you won't often need to call add_node() directly. The following code adds nodes and edges so that the network matches the example network from earlier and creates a new visualization:

G.add_edges_from([('B', 'D'), ('C', 'E')]) 
nx.draw_networkx(G)

The draw_networkx() function now produces a visualization including the new D and E nodes:

Output of draw_networkx() after adding nodes
Previous PageNext Page
You have been reading a chapter from
Network Science with Python and NetworkX Quick Start Guide
Published in: Apr 2019Publisher: PacktISBN-13: 9781789955316
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Edward L. Platt

Edward L. Platt creates technology for communities and communities for technology. He is currently a researcher at the University of Michigan School of Information and the Center for the Study of Complex Systems. He has published research on large-scale collective action, social networks, and online communities. He was formerly a staff researcher at the MIT Center for Civic Media. He contributes to many free/open source software projects, including tools for media analysis, network science, and cooperative organizations. He has also done research on quantum computing and fault tolerance. He has an M.Math in Applied Mathematics from the University of Waterloo, as well as B.S degrees in both Computer Science and Physics from MIT.
Read more about Edward L. Platt