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.

1
  • 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. stackoverflow.com/q/6570635) Commented Nov 30, 2015 at 18:40

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

Works for me. So cool. Now if it had the d3js force layout on top of it, that would be really something!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.