Python 2.7 NetworkX(使其具有交互性) [英] Python 2.7 NetworkX (Make it interactive)
问题描述
我是NetworkX的新手.现在,我设法将所有节点连接到该特定节点.我接下来要做的是使其具有交互性,例如能够通过使用光标拖动来使每个节点移动.我知道我必须使用matplotlib,但是我不确定如何使用它.谁能帮我吗?
I am new to NetworkX. Right now, I manage to connect all the nodes to this particular node. What I want to do next it to make it interactive e.g. able to make each of the node move by dragging using cursor. I know I have to make use of matplotlib, but I am not sure how to use it. Can anyone help me?
我的代码是:
import matplotlib.pyplot as plt
import networkx as nx
import itertools
d = [name of nodes]
f = [number per nodes]
for i in d:
G.add_edge('"' + i + '"',b)
pos=nx.fruchterman_reingold_layout(G, k=0.5, iterations=5)
nx.draw_networkx_nodes(G,pos,node_size=130, node_color="white")
nx.draw_networkx_edges(G,pos, width=0.2,alpha=1,edge_color='black')
nx.draw_networkx_labels(G,pos,font_size=7,font_family='sans-serif')
for i,j in itertools.izip(d,f):
nx.draw_networkx_edge_labels(G,pos, {('"' + i + '"',b):j}, font_size=7, label_pos= 0.80)
plt.axis('off')
plt.show()
推荐答案
使用matplotlib似乎很困难(实际上并不是为此而设计的). Networkx绘图模块非常差,它主要使用节点等的自定义散点图.
It seems hard to do with matplotlib (it is not really been designed for that). Networkx drawing module is pretty poor it mostly uses a custom scatter plot for nodes, etc.
我建议另一种解决方案:
I suggest another solution:
- 将图形导出为JSON或GEXF,并使用Javascript图形绘图库使图形具有交互性,例如: SigmaJs ,或 VivaGraphJs .
- Export your graph to JSON or GEXF and use a Javascript graph drawing library to make your graph interactive such as: SigmaJs, or VivaGraphJs.
您在我的网页上找到了一个嵌入NetworkX的真实图形示例.博客.在此示例中,节点是静态的,但是单击节点会突出显示其邻居.
You find an example of a real graph created with NetworkX embedded on a webpage on my blog. Nodes are static in this example but clicking on a node highlights its neighbors.
建议的交互式图形绘图库的官方示例:
Official examples for the proposed interactive graph drawing libraries:
这篇关于Python 2.7 NetworkX(使其具有交互性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!