How to Visualise and Draw Networks in Python #
So far in this series, we’ve covered everything from creating a graph to analysing it, but we haven’t looked at visualising networks yet. To wrap things up, this guide is all about walking you through the process of visualising networks using the handy tools packed into NetworkX.
Layouts #
Before we can draw the graph, we need to decide on a layout. A layout helps us figure out how the nodes and edges will be positioned on the canvas. There are loads of different layouts to choose from, depending on the structure of your network. Here are a few options you might want to consider:
- Spring layout: This is one of the most popular layout algorithms and uses a force-directed approach. It positions the nodes based on forces that pull connected nodes together while pushing others apart, giving a natural feel to the layout. You can use it with
nx.spring_layout(G)
. - Bipartite layout: This one arranges nodes into two groups, with each set of nodes lined up in straight lines. You’ll need to specify which nodes belong to which group. Use it with
nx.bipartite_layout(G, nodes)
. - Circular layout: This layout puts the nodes in a circular formation, which can be quite useful. You can apply it with
nx.circular_layout(G)
. - Spectral layout: This is handy for positioning highly clustered nodes close together. You can use it with
nx.spectral_layout(G)
.
Other noteworthy mentions include:
- Kamada-Kawai layout: Use it with
nx.kamada_kawai_layout(G)
. - Planar layout: Use it with
nx.planar_layout(G)
. - Random layout: You can apply it with
nx.random_layout(G)
. - Shell layout: Use it with
nx.shell_layout(G)
.
Using Layouts in NetworkX #
When drawing graphs in NetworkX, layouts are used to position nodes on the canvas in a particular way. These layouts make it easier to visualise the structure of a network. NetworkX provides several built-in layouts like circular, spring, shell, and random layouts. Here’s a super simple example using the spring_layout
and circular_layout
.
import networkx as nx
import matplotlib.pyplot as plt
# Create a simple graph
G = nx.Graph()
G.add_edges_from([(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (3, 4)])
# Apply the spring layout (nodes are arranged with repulsive forces)
pos_spring = nx.spring_layout(G)
# Apply the circular layout (nodes arranged in a circle)
pos_circular = nx.circular_layout(G)
# Draw the graph using the spring layout
plt.figure(figsize=(8, 4))
# Subplot 1: Spring layout
plt.subplot(121)
nx.draw(G, pos_spring, with_labels=True, node_color="lightblue", node_size=500, font_size=10)
plt.title("Spring Layout")
# Subplot 2: Circular layout
plt.subplot(122)
nx.draw(G, pos_circular, with_labels=True, node_color="lightgreen", node_size=500, font_size=10)
plt.title("Circular Layout")
plt.show()
In this example, we’ve created a simple undirected graph and used two different layouts to position the nodes. This produces the following result.
Conclusions #
Now that we’ve got everything sorted, you can pull it all together and create some pretty impressive data visualisations. This quick guide gives you the essential tools you need to get started.
Of course, there’s way more to network visualisations than just this. I aimed to keep this post short and sweet, providing just enough material to kick things off. There’s a ton more that could’ve been included, but that would’ve needed a whole series of guides just to scratch the surface.