mpld3 networkx d3.js force layout

mpld3 is a matplotlib to d3 library. It is lightweight and a pure Python / Javascript package, allowing a lot of the matplotlib interface to be accessible in the web. There are a number of examples on their website. Its integration with d3 allows someone familiar with Javascript to use Python and visualize using the power of d3. d3.js is a powerful low level visualization library and there are loads of examples online on the many features it brings to the table.

mpld3 also has the ability to add plugins to add new functionality. I wanted to take a shot at adding a d3.js force layout plugin. The force layout is a powerful visualization tool and NetworkX has a nifty function that will convert the graph along with its attributes into a JSON graph format. I’d played around with this before and figured this would be a nice feature to have, so I’ve worked on it over the weekend and here it is - a NetworkX to d3.js force layout plugin for mpld3. I’ve shared an example below.

I’ve implemented a sticky version of the force layout, since this is what I wanted. This can be turned off by passing an argument to the plugin. The reason it is called a sticky version is because dragging a node to a new position will fix it at that location. You can double click the node to release it.

These blocks were used as a reference.1

I’ll run through an explanation of the code briefly.

This returns a figure and axes object. This plugin requires a single axes object to be passed to it. The figure and axes object, and everything that is on the axes object is converted to mpld3 plot objects. In theory, you could use NetworkX’s draw function to visualize the graph and mpld3 will render it fine. The only downside to that is that the final output will not be a force layout.

Next we create a graph with the following commands

I’ve set the color attribute of the first node to red. This is an attribute on the node object and will be used by the force layout to color the node. We can also set the (x, y) coordinates to values for the first and second node. Passing the fixed=True keyword argument assigns a attribute fixed with the value True on the NetworkX graph. When converted to a force layout, this will fix the positions of those nodes.

We are almost done! This registers the plugin with mpld3.

The pos argument passed here is None. I plan to set it up such that you can pass a position dictionary to the plugin and have the plugin assign (x,y) coordinates when available. You can generate the pos dictionary using any of NetworkX’s layout functions.

Additional keywords arguments can be passed to the constructor of the NetworkXD3ForceLayout class. This allows a user to control certain force layout properties like gravity, linkDistance, linkStrength etc. You can also set a default node size or turn off the dragging feature. The full list of attributes that can be passed is found in the docstring. I plan to write a more detailed description in a following post.

Here is another example of a NetworkX graph converted to a force layout. This is Zachary’s Karate Club. Nodes in Mr Hi’s club are coloured purple and the rest are coloured orange. Node size is also changed based on the number of neighbours.

  1. ???; ???