How to Visualise and Draw Networks in Python

How to Visualise and Draw Networks in Python
Photo by Omar Flores / Unsplash

So far in this series, we've gone through everything from creating a graph to analysing it. We have yet to visualise networks. To conclude this series, this blog post sets out to walk through the process of visualising networks using tools packaged into networkx.

Layouts

Before we draw the graph, we need to define a layout. A layout is used to determine how nodes and edges are positioned on the canvas. There are many different types of layouts to use depending on your network structure. A selection of these layouts are listed as follows:

  • Spring layout : One of the most popular layout algorithms which uses a force-directed layout. Edges act as springs by pulling nodes together. nx.spring\_layout(G)
  • Bipartite layou t: Used to position nodes into two groups. The two sets of nodes are positioned into two straight lines. Requires a set of nodes to partition to one side. nx.bipartite_layout(G, nodes)
  • Circular layout : Positions nodes into a circular formation. nx.circular_layout(G)
  • Spectral layout : Used to position highly clustered nodes together. nx.spectral_layout(G)

Other honourable mentions include...

  • Kamada-Kawai layout : nx.kamada_kawai_layout(G)
  • Planar layout : nx.planar_layout(G)
  • Random layout : nx.random_layout(G)
  • Shell layout : nx.shell_layout(G)

Drawing

Now that we selected our layout, we can begin to draw the networks. Drawing networks using networkx is done in two parts. Both nodes and edges are drawn separately. This is done so that the analyst has complete control over how the network is presented.

Here's a quick example to get things going. To begin, we can draw our nodes using two parameters: The graph G and the layout configuration we defined earlier pos.

>>> nx.draw_networkx_nodes(G, pos)

A complete set of customisable parameters can be found here .

Drawing the edges can be done in a similar fashion. This also takes the graph G and the layout pos . Likewise, a complete set of customisable parameters can be found here .

>>> nx.draw_networkx_edges(G, pos)

Conclusions

Now that we've got everything we need, you can put it all together and make some pretty Now that we've got everything we need, you can put it all together and make some pretty impressive data visualisations. This brief blog post provides the very basic tools need to get started. As you can imagine, there is far more to network visualisations than just this. I made this blog post as small as possible with just enough material to get things started. There is so much more that could have been added but would have required a whole series of blog posts just to scratch the surface.