Interactive labels on nodes using python and networkx
Question: 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](http://d3js.org/) seems able to do this well, and there are a couple of
python implementations

[mpld3](http://mpld3.github.io/examples/scatter_tooltip.html)

and

[Drew Conway's Networkx fork](https://github.com/drewconway/networkx)

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 ](http://drewconway.com/zia/2013/3/26/visualizing-
networkx-graphs-in-the-browser-using-d3) 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.


Answer: 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()
    

