首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用pyvis更改子图的颜色?

如何使用pyvis更改子图的颜色?
EN

Stack Overflow用户
提问于 2020-09-03 13:00:08
回答 1查看 2.1K关注 0票数 1

我想用pyviz来可视化这个图形。我正在编写从图中提取子图的代码。

代码语言:javascript
复制
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重建这个可视化的图像。有人能帮我完成这个吗?

EN

回答 1

Stack Overflow用户

发布于 2020-09-03 17:20:00

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

代码语言:javascript
复制
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

确保图形和子图形都属于同一类型:

代码语言:javascript
复制
G = nx.from_numpy_matrix(m, create_using=nx.MultiDiGraph())
Gk = nx.MultiDiGraph()  # changed the graph type to MultiDiGraph

最后,你将拥有:

代码语言:javascript
复制
# VISUALIZATION
visualize(G, Gk)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63717126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档