python - Group vertices in clusters using NetworkX -
i trying represent graphically graphs, , need group in clusters nodes have common characteristics.
i using networkx, , need similar graph this tutorial, slide 44, left figure.
i want draw delimiting line around each cluster. current code that:
vec = self.colors colors = (linspace(0,1, len (set (vec)))* 20 + 10) nx.draw_circular(g, node_color=array([colors[x] x in vec])) show ()
i wish find example , see how can use networkx cluster graph.
i'm not positive question is. think you're asking "how networkx put nodes close together"
before launch answer, drawing documentation networkx here: http://networkx.lanl.gov/reference/drawing.html
so figure you're asking has 4 different communities clustered based on having lots of edges within each community , not many outside.
if don't want put effort it, spring_layout putting tightly knit communities together. basic algorithm of spring_layout acts if edges springs (and nodes repel). lots of edges keeps nodes close together. note initializes positions randomly, each time you'll different output.
the easiest way just
nx.draw_spring(g)
but maybe want more. if want to, can fix every single node's position. define dict, named pos.
pos = {} node in g.nodes_iter(): pos[node] = (xcoord, ycoord).
where xcoord , ycoord coordinates want node at.
then draw_networkx(g, pos = pos)
that's lot of effort. tell few of them have in particular places, , let networkx rest
define fixedpos few nodes , run spring_layout telling nodes fixed , giving fixedpos initial positions. hold fixed , fit else around.
here code generates network has 4 connected parts , few other edges between them. (actually generates complete network , deletes few edges between these parts). draws simple spring layout. fixes 4 of them @ corners of square , places other nodes around fixed positions.
import networkx nx import random import pylab py math import floor g = nx.complete_graph(20) edge in g.edges(): if floor(edge[0]/5.)!=floor(edge[1]/5.): if random.random()<0.95: g.remove_edge(edge[0],edge[1]) nx.draw_spring(g) py.show() fixedpos = {1:(0,0), 6:(1,1), 11:(1,0), 16:(0,1)} pos = nx.spring_layout(g, fixed = fixedpos.keys(), pos = fixedpos) nx.draw_networkx(g, pos=pos) py.show()
you can specify weights edges, pass weights spring_layout , larger weights tell keep corresponding nodes closer together. once you've identified communities, increase weights within communities/clusters if necessary keep them close together.
note can specify color make each node, straightforward specify color each community/cluster.
if want draw curves around each of clusters, you'll have through matplotlib.
Comments
Post a Comment