Python 2.7 NetworkX (Make it interactive)

2024/10/11 10:19:00

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 itertoolsd = [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()
Answer

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:

  • List of examples using sigma.js.

  • Tutorial for VivaGraphJs.

https://en.xdnf.cn/q/69781.html

Related Q&A

Normal Distribution Plot by name from pandas dataframe

I have a dataframe like below:dateTime Name DateTime day seconds zscore 11/1/2016 15:17 james 11/1/2016 15:17 Tue 55020 1.158266091 11/1/2016 13:41 james 11/1/2016 13:41 Tue 4926…

Change pyttsx3 language

When trying to use pyttsx3 I can only use English voices. I would like to be able to use Dutch as well. I have already installed the text to speech language package in the windows settings menu. But I …

pandas groupby dates and years and sum up amounts

I have pandas dataframe like this:d = {dollar_amount: [200.25, 350.00, 120.00, 400.50, 1231.25, 700.00, 350.00, 200.25, 2340.00], date: [22-01-2010,22-01-2010,23-01-2010,15-02-2010,27-02-2010,07-03-201…

Is Python on every GNU/Linux distribution?

I would like to know if is Python on every G/L distribution preinstalled or not. And why is it so popular on GNU/Linux and not so much on Windows?

Installing QuantLib in Anaconda on the Spyder Editor (Windows)

How do I install the QuantLib Package in Anaconda. I have tried the following code;import QuantLib as qlbut I am getting the following result;ModuleNotFoundError: No module named QuantLibCan anyone ass…

get rows with empty dates pandas python

it looks like this:Dates N-D unit 0 1/1/2016 Q1 UD 1 Q2 UD 2 Q3 UD 3 2/1/2016 Q4 UD 4 5/1/2016 Q5 UD 5 Q6 UDI want to filter out the empty Dates row…

Python: Gridsearch Without Machine Learning?

I want to optimize an algorithm that has several variable parametersas input.For machine learning tasks, Sklearn offers the optimization of hyperparameters with the gridsearch functionality.Is there a …

Pandas division (.div) with multiindex

I have something similar to thisdf = pd.DataFrame(np.random.randint(2, 10, size = (5, 2))) df.index = pd.MultiIndex.from_tuples([(1, A), (2, A), (4, B), (5, B), (8, B)]) df.index.names = [foo, bar] df.…

Add a delay to a specific scrapy Request

Is it possible to delay the retry of a particular scrapy Request. I have a middleware which needs to defer the request of a page until a later time. I know how to do the basic deferal (end of queue), a…

importing without executing the class - python

my problem is about i have a file that contain class and inside this class there is bunch of code will be executed so whenever i import that file it will executed ! without creating an object of the…