创建动态图 python NetworkX [英] creating dynamic graph python NetworkX

查看:175
本文介绍了创建动态图 python NetworkX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 

推荐答案

我不确定这是否是您所说的动态,但也许是这样的?

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

产生输出:

普通静态图

您可以与之交互的动态图

这篇关于创建动态图 python NetworkX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆