3D Network Visualisations in Deepnote
In this article I am going to take you through how to create interactive 3D network visualisations in Deepnote using Plotly. This is an image of the beautiful final 3D visualisation of Zachary's Karate Club that we are working towards:
This article is going to be all about network visualisations, or more specifically how to create ineractive 3D networks in deepnote. We are going to do this using the python packages networkx and Plotly. If you are interestd in the fundamentals of networks and their potential applications do have a look at my last article on social network analysis.
We are going to walk through how to produce a 3D visualisation of the Zachary Karate Club graph that we worked with in my last article.
In my last post we used networkx to make our visualisations. That just about did the job, but now are going to explore using Plotly to make some more sophisticated visualisations. The open source software Gephi is also often used for visualisations, but Plotly is usually easier to use as it means we can do everything in python and never have to leave our Deepnote notebook!
The first step is to get all of the python packages we need installed and imported. We will also open the Zachary's Karate Club (ZKC) graph, and set-up some variables to hold other useful information.
In the visualisation we want to distinguish between the different communities detected in my last article. We want to visualise these two communities (our predictions for which faction each individual will join) in a different colour. For ease they are included here:
Visualisation using NetworkX
As a reminder of what we are able to do using networkx lets create a quick 2D visualisation.
A convenient feature of using networkx for plots is that we can very easily choose the positions of our nodes using different networkx functions. Last time we used a circular layout, this time we will use a spring layout.
The benefit of this spring layout is that it makes the distinctions between the two detected communities very clear. We can see that both of the communities are clustered around their 'leader' (Mr Hi or John A.).
This matplotlib plot however is ultimately limited, it is tightly packed and it's hard to tell exactly what is going on. To improve on this let's see what can be done with Plotly.
On to Plotly!
It's really easy to use Plotly in deepnote to create a 3D visualisation of a network. Essentially, we are going to make seperate 3D scatter plots (or traces in common Plotly terminology) of the nodes and the edges which will then be plotted together.
Before diving into creating a trace for the nodes I want to highlight one of Plotly's most useful features: Hover Labels. It lets the user to reveal more information about a data point by moving their mouse cursor over the point and having a hover label appear. Here we set the hoverinfo to 'text' and the text to the club labels that we retreived at the beginning.
That means in the visualisation we will be able to find out which faction an individual ended up joining just by hovering our mouse close to the node.
To clearly highlight Mr Hi and John A. We need to plot their nodes again in another colour. The simplest way to do this is to create another trace for each node. These will then be plotted on top of the other nodes.
Now we have got everything sorted here is our plot!
Great! So there we have it. A 3D visualisation of our Zachary Karate Club graph. The two communities (purple and green) represent our predictions for which faction each individual will join. Whilst the dark purple and dark green nodes represent Mr Hi and John A respectively.
If you hover your mouse over the different nodes it will also tell you which faction the individual actually chose to join. In my last article our predictions were wrong for two individuals. See if you can find them (hint they are near the centre of the graph).
What about a more complicated example?
The internet is a typical example when it comes to complex networks. In fact, it is this insight, modelling the internet as a network, that allows Google to return effective search results.
Below is a 3D visualisation of a network 2000 different webpages, generated from networkx (feel free to play around with it, it's also interactive!). The colors represent different communities that were detected.
That's it for this article, but I hope it has given you everything you need to know to open up your own Deepnote notebook and have a play around with your own visualisations!