创建动态图形python NetworkX

时间:2017-11-18 22:19:05

标签: python graph plotly networkx

我正在尝试使用networkX在python中构建动态图。我有一些代码来构建静态图。我正在寻找一些建议,如何改变动态图形以改善可视化,可能使用networkx d3或plotly。上下文是绘制对话图。

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 

1 个答案:

答案 0 :(得分:1)

我不确定这是否是动态的意思,但也许是这样的话?

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')])])

哪个产生输出:

Normal static graph

Dynamic graph you can interact with