2

I am trying to make a graph using python with networkx which has many nodes that can be interactively investigated. I want to be able to click or hover above a node and reveal a label which is otherwise not shown.

D3 seems able to do this well, and there are a couple of python implementations

mpld3

and

Drew Conway's Networkx fork

mpld3 works fine for scatter plots but I don't know how to get it to do what I want for a graph...

implementing example code from Drew Conway:

import networkx as nx  
from networkx.readwrite import d3_js

gives

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name d3_js

This looks like an error which might have resulted if the forked networkx package was not placed in python's system path....However, I checked the sys path contents and found networkx...so I'm stumped.

user1278616
  • 103
  • 2
  • 6
  • Do you have `networkx` installed both as a fork and as a standard package? The fork will need to come first in the python path - but >1 version of a package likely needs special care (see e.g. http://stackoverflow.com/q/6570635) – Bonlenfum Nov 30 '15 at 18:40

1 Answers1

2

It looks like mpld3 will work. You can get the scatter data by calling draw_networkx_nodes() which is just a wrapper for scatter().

import matplotlib.pyplot as plt
import numpy as np
import mpld3

import networkx as nx
G = nx.path_graph(4)
pos = nx.spring_layout(G)

fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'))
scatter = nx.draw_networkx_nodes(G, pos, ax=ax)
nx.draw_networkx_edges(G, pos, ax=ax)

labels = G.nodes()
tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels)
mpld3.plugins.connect(fig, tooltip)

mpld3.show()
Aric
  • 24,511
  • 5
  • 78
  • 77
  • [Works for me](http://nbviewer.ipython.org/gist/aflaxman/612ba0293714f2e5f3f9). So cool. Now if it had the d3js [force layout](https://github.com/mbostock/d3/wiki/Force-Layout) on top of it, that would be really something! – Abraham D Flaxman Nov 30 '15 at 20:04