Skip to content

Instantly share code, notes, and snippets.

@pravj
Last active November 29, 2017 11:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pravj/53a5dc8e3b72cc2dae3f to your computer and use it in GitHub Desktop.
Save pravj/53a5dc8e3b72cc2dae3f to your computer and use it in GitHub Desktop.
The basic version of "octogrid", to visualize communities in GitHub following network
import igraph as ig
import plotly.plotly as py
from plotly.graph_objs import *
G = ig.Graph.Read_GML('network.gml')
labels = list(G.vs['label'])
N = len(labels)
E = [e.tuple for e in G.es]
layt = G.layout('graphopt')
community = G.community_multilevel().membership
communities = len(set(community))
color_list = ['#6959CD', '#DD5E34', '#69CD45', '#000005']
Xn = [layt[k][0] for k in range(N)]
Yn = [layt[k][1] for k in range(N)]
Xe = []
Ye = []
for e in E:
Xe+ = [layt[e[0]][0], layt[e[1]][0], None]
Ye+ = [layt[e[0]][1], layt[e[1]][1], None]
trace1 = Scatter(x=Xe,
y=Ye,
mode='lines',
line=Line(color='rgb(210,210,210)', width=1),
hoverinfo='none'
)
node_x = [[] for i in range(communities)]
node_y = [[] for i in range(communities)]
labelz = [[] for i in range(communities)]
for j in range(len(community)):
index = community[j]
node_x[index].append(layt[j][0])
node_y[index].append(layt[j][1])
labelz[index].append(labels[j])
plot_data = [trace1]
for i in range(communities):
trace = Scatter(x=node_x[i],
y=node_y[i],
mode='markers',
name='ntw',
marker=Marker(symbol='dot',
size=5,
color=color_list[i],
line=Line(color='rgb(50,50,50)', width=0.5)
),
text=labelz[i],
hoverinfo='text'
)
plot_data.append(trace)
axis=dict(showline=False, # hide axis line, grid, ticklabels and title
zeroline=False,
showgrid=False,
showticklabels=False,
title=''
)
width = 800
height = 800
layout = Layout(title= "GitHub Network",
font=Font(size=12),
showlegend=False,
autosize=False,
width=width,
height=height,
xaxis=XAxis(axis),
yaxis=YAxis(axis),
margin=Margin(
l=100,
r=40,
b=85,
t=100,
),
hovermode='closest',
annotations=Annotations([
Annotation(
showarrow=False,
text='This igraph.Graph has the graphopt layout',
xref='paper',
yref='paper',
x=0,
y=-0.1,
xanchor='left',
yanchor='bottom',
font=Font(
size=14
)
)
]),
)
data = Data(plot_data)
fig = Figure(data=data, layout=layout)
py.plot(fig, filename='github-network-community-igraph')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment