1

我想使用 pyviz 可视化图表。我正在研究从图中提取子图的代码。

import networkx as nx
import numpy as np
import matplotlib.pyplot as plt

def RWS(G, vs, r=0.5, S=4):
    #initialize subgraph
    Gk = nx.DiGraph()
    #initialize nodes 
    Vk = [] 

    #add vs to Gk
    Gk.add_node(vs) 
    Vk.append(vs)

    while len(Vk) < S:
        #get neighbor nodes set of Vk (step 4) (Also appending j just for the purpose of adding edge)
        NS = [(n, j) for j in Vk for n in G.neighbors(j) if n not in Vk]
        print("{} {} {} {}".format('length of NS is', len(NS), 'and vs =', vs))
        # randomly select r of nodes in NS, add them into the Vk
        if not len(NS) == 0:
            for node, j in NS:
                if np.random.uniform() < r:
                    Vk.append(node)
                    Gk.add_edge(j, node)
                    if len(Vk) == S or len(NS) < S:
                        break
        else:
            break
    return Gk


if __name__ == '__main__':

    m = np.matrix([
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [1, 0, 0, 0, 0, 1, 0, 0, 0, 0], 
    [0, 1, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 1, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 1, 0, 1, 1, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 1, 0, 0], 
    [0, 0, 0, 0, 0, 0, 0, 1, 0, 0]])

    # G = nx.from_numpy_matrix(m, create_using=nx.MultiDiGraph())
    G =  nx.from_numpy_matrix(m, create_using=nx.DiGraph)
    #expansion ratio
    r  = 0.5
    #subgraph size
    S  = 4
    # randomly select the node from G
    vs = np.random.randint(0, G.size()) 
    print(vs)

    Gk = RWS(G, vs, r, S)


    # VISUALIZATION
    pos = nx.spring_layout(G)
    nx.draw_networkx_nodes(G, pos)
    nx.draw_networkx_nodes(G, pos, nodelist=list(Gk.nodes()), node_color='r')
    nx.draw_networkx_labels(G, pos)
    nx.draw_networkx_edges(G, pos, edge_color='b', width=0.5)
    nx.draw_networkx_edges(G, pos, edgelist=list(Gk.edges()), edge_color='g', width=1, arrowstyle='->')

    plt.axis('off')
    plt.show()

结果 在这里,我有 networkx 格式的图,并从中提取子图,然后使用 networkx 进行可视化。为了便于查看,子图为绿色。但是,这种可视化不适用于大型图。

所以我想使用 pyvis 重新创建它的可视化。有人可以帮我完成这个吗?

4

1 回答 1

1

为了在 中可视化此图pyvis,您可以编写以下函数:

from pyvis.network import Network


def visualize(G, SG):
    N = Network(height='100%', width='100%', bgcolor='#222222', font_color='white', directed=True)
    # uncomment the following if the graph is large
    # N.barnes_hut(spring_strength=0.006)

    for n in G:
        if n in SG:  # if the node is part of the sub-graph
            color = 'green'
        else:
            color = 'red'
        N.add_node(n, label=n, color=color)

    for e in G.edges:
        if e in SG.edges:  # if the edge is part of sub-graph
            color = 'green'
            width = 2
        else:
            color = 'red'
            width = 0.5
        N.add_edge(int(e[0]), int(e[1]), color=color, width=width)

    N.write_html('subgraph.html')  # save a html file in current dir

确保图和子图都是同一类型:

G = nx.from_numpy_matrix(m, create_using=nx.MultiDiGraph())
Gk = nx.MultiDiGraph()  # changed the graph type to MultiDiGraph

最后你将拥有:

# VISUALIZATION
visualize(G, Gk)

在此处输入图像描述

于 2020-09-03T09:20:00.480 回答