Isaac
Isaac

Reputation: 215

creating dynamic graph python NetworkX

I am attempting to build a dynamic graph in python using networkX. I've got some code to build a static graph. I'm looking for some advice as to how to alter it for dynamic graphing to improve the visualization, maybe using networkx d3 or plotly. The context is to graph a conversation.

nx.draw_networkx(speech, pos=nx.spring_layout(speech))
plt.draw()
static_images_dir = "./static/images"
if not os.path.exists(static_images_dir):
    os.makedirs(static_images_dir)
plt.savefig(os.path.join(static_images_dir, "speech.png"))
#plt.show()
plt.close()
return speech 

Upvotes: 1

Views: 2221

Answers (1)

Kim Tang
Kim Tang

Reputation: 2478

I am not sure if that's what you mean by dynamic, but maybe something like this?

import networkx as nx
import numpy as np
import matplotlib.pylab as plt
import hvplot.networkx as hvnx
import holoviews as hv
from bokeh.models import HoverTool
hv.extension('bokeh')

A = np.matrix([[0,1,1,0,0],[1,0,1,0,0],[1,1,0,1,1],[0,0,1,0,1],[0,0,1,1,0]])
G = nx.from_numpy_matrix(A)
pos = nx.spring_layout(G)

nx.draw_networkx(G, pos, node_color='lightgray')
plt.show()

hvnx.draw(G, pos, node_color='lightgray').opts(tools=[HoverTool(tooltips=[('index', '@index_hover')])])

Which produces the output:

Normal static graph

Dynamic graph you can interact with

Upvotes: 2

Related Questions