简体   繁体   中英

Interactive labels on nodes using python and networkx

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM