代码之家  ›  专栏  ›  技术社区  ›  Isaac

创建动态图python NetworkX

  •  1
  • Isaac  · 技术社区  · 7 年前

    我试图使用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 回复  |  直到 7 年前
        1
  •  2
  •   Kim Tang    5 年前

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

    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