Python 2.7 NetworkX (Make it interactive)
Asked Answered
S

2

7

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?

enter image description here

My codes are:

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()
Selassie answered 23/7, 2015 at 7:11 Comment(2)
Probably a hard thing to do. Here is one place where I know someone managed to get it to respond to mouse clicks (to delete a node). #30289849Slating
Is there any module other than matplotlib able to do this?Selassie
C
6

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:

  • Export your graph to JSON or GEXF and use a Javascript graph drawing library to make your graph interactive such as: SigmaJs, or VivaGraphJs.

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:

Chanachance answered 24/7, 2015 at 9:43 Comment(0)
L
4

Matplotlib was designed more for static graphs and charts.

However once the NetworkX graph is exported to GEXF format there is a tool which will allow you to select areas based on position or critera in order to move it around. The tool is called Gephi. You can play with the layout to get started or go as deep as data scientists like to get.

Lichenology answered 27/7, 2015 at 0:11 Comment(1)
Tried using Gephi. It is incredible. Thanks! Anyway, is it possible to export the graph in Gephi to web browser using Python 2.7?Selassie

© 2022 - 2024 — McMap. All rights reserved.