创建动态图形python NetworkX

2024-06-11 18:04:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用networkX在python中构建一个动态图。我有一些代码来建立一个静态图形。我正在寻找一些建议,如何改变它的动态绘图,以改善可视化,也许使用networkxd3或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 

Tags: path代码networkx图形osdir静态static
1条回答
网友
1楼 · 发布于 2024-06-11 18:04:03

我不确定你所说的动态是不是这个意思,但也许是这样的?在

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

相关问题 更多 >