Package: d3fdgraph
Plots an interactive force directed graph in a jupyter notebook, taking data from a dataframe of nodes and link weights.Why d3fdgraph?Working with data usingpythonin thejupyternotebook provides many options for visualising that data.Thepandaslibrary provides convenient common visualisations, and there’s always the venerablematplotlibfor more bespoke plots. The newvega-litelibrary is enabled by default in jupyterlab.Sometimes we want to plot the relationships between things as a graph of linked nodes.Thenetworkxlibrary can plot graphs in a notebook but the plots are static, and only really suitable for smaller data.Being able tointeractivelyadjust graphs really helps explore larger data.That’s whatd3fdgraphdoes.Installing d3fdgraphInstallingd3fdgraphis easy.pip install d3fdgraphThis has been tested with Anaconda Python 3.How To UseTo used3fdgraphin a notebook we import the library.import d3fdgraphd3fdgraphtakes data in the form of apandas dataframe.That dataframe must have 3 columns:The first column contains the nodes at thestartof a link.The second column contains the nodes at theendof a link.The third column contains a linkweightnumber. It must be more than 0.The larger theweight, the more strongly related the two nodes at each end of this link.Here is an example dataframe:sourcetargetweightappleorange1applebanana2orangemango2orangelemon3Each row is a link:The first row is a link betweenappleandorange. These two are connected with a weight of 1.The second row is a link betweenappleandbanana. These two are more strongly related, with a weight 2.The fourth row is the strongest link, with weight 3, betweenorangeandlemon.The column names don’t have to besource,targetandweight. You can have different column names, but it is a good idea to use descriptive names.The order of the 3 columns is what matters.d3fdgraphwill use the first column as the source nodes, the second as the target, and the third as link weights.To draw an interactive force directed graph of these nodes and links we simply pass this dataframe to theplot_force_directed_graph()function.d3fdgraph.plot_force_directed_graph(dataframe)Here is an example of a graph made from different data.The nodes are labelled with the names of the nodes, as described in the pandas dataframe.The nodes are constrained from flying off the edge of the canvas.InteractivityYou can use your pointer to drag nodes around to adjust the graph.Double-clicking a node makes it jump to the centre of the canvas, from where it might depart slightly due to the simulated forces.Additional Parametersplot_force_directed_graph(node1_node1_weight, node_radius=3, link_distance=20, collision_scale=4)You don’t have to use the additional parameters, but they might help improve the readability of your graph.node_radiusis the radius of the circles drawn for each node, default 3link_distanceis the length of each link, before divided by the link weight, default 20collision_scaleis the multiple of the node radius that excludes other nodes, default 4link_width_scalescales the width of the drawn links with link weight, default is 4Example NotebookA simple jupyter notebook showing howd3fdgraphcan be used is here:simple example notebookNoteNote thatd3fdgraphis only useful in a jupyter notebook. It isn’t useful in a terminal, and hasn’t been tested in other kinds of python notebook.