简体   繁体   中英

How to draw a graph with community structure in pyvis

I have an edge list which nodes are string values such as cities name. After applying community detection algorithm on them, i got cities with labels, that cities with same label are grouped in same community. i want to visualize the graph of cities where cities with same label get colored with same color. can any one help me with this? i dont know even pyvis has this feature or not?

i am able to draw a graph using this code:

from pyvis.network import Network
net = Network(height='800px', width='800px', directed=False, notebook=True)
net.from_nx(G)
net.show("example.html")

But i dont know how to append labels to nodes based on the communities detected.

When creating your pyvis graph, you can give each community a unique color by giving each node in that community the same color. Something like:

import random
r = lambda: random.randint(0,255)

# Loop over each community
for community in graph:
    communityColorString = '#%02X%02X%02X' % (r(),r(),r())
    
    # Loop over each node 
    for node in community:
        newGraph.add_node(word_a, word_a, title=word_a, color=communityColorString)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM