-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3d_network_plot.py
198 lines (150 loc) · 4.23 KB
/
3d_network_plot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# coding: utf-8
"""
Created on Thu May 13 19:20:43 2021
@author: Shaunak_Sensarma
"""
# In[7]:
import igraph as ig
import json
from urllib.request import urlopen
content = urlopen("https://raw.githubusercontent.com/plotly/datasets/master/miserables.json")
data = []
"""req = urllib2.Request("https://raw.githubusercontent.com/plotly/datasets/master/miserables.json")
opener = urllib2.build_opener()
f = opener.open(req)"""
f=content
data = json.loads(f.read())
# In[16]:
L=len(data['links'])
Edges=[(data['links'][k]['source'], data['links'][k]['target']) for k in range(L)]
Gp=ig.Graph(Edges, directed=False)
# In[19]:
print((Edges[0]))
# In[3]:
labels=[]
group=[]
for node in data['nodes']:
labels.append(node['name'])
group.append(node['group'])
# In[25]:
from igraph import *
G=Graph()
def addVertex(g,name_str):
try:
if(name_str not in g.vs['name']):
g.add_vertex(name=name_str)
except KeyError:
g.add_vertex(name=name_str)
return g
def write_tuple_to_file(f,t):
string=str(t[0])+' '+str(t[1])+'\n'
f.write(string)
def retrieve_edge_name_tuple(g,t):
a=(g.vs[t[0]]['name'],g.vs[t[1]]['name'])
return a
def load_dataset(fileName,g):
fileNums=[0]
for i,eachNum in enumerate(fileNums):
print(eachNum)
fileName="Datasets/facebook/edges/"+str(eachNum)+".edges"
print('fileName=',fileName)
f=open(fileName)
line=f.readline()
while(line!=''):
c=(line.split())
g=addVertex(g,c[0])
g=addVertex(g,c[1])
print('Adding ',c[0],'-->',c[1])
g.add_edge(c[0],c[1])
line=f.readline()
g.simplify()
return
load_dataset('abd',G)
N=len(G.vs)
layt=G.layout('kk', dim=3)
labels=[]
print(type(labels))
for eachNde in G.vs:
labels.append(eachNde['name'])
Edges=list()
print(type(Edges))
for eachTuple in G.es:
Edges.append(eachTuple.tuple)
Xn=[layt[k][0] for k in range(N)] # x-coordinates of nodes
Yn=[layt[k][1] for k in range(N)] # y-coordinates
Zn=[layt[k][2] for k in range(N)] # z-coordinates
Xe=[]
Ye=[]
Ze=[]
for e in Edges:
Xe+=[layt[e[0]][0],layt[e[1]][0], None] # x-coordinates of edge ends
Ye+=[layt[e[0]][1],layt[e[1]][1], None]
Ze+=[layt[e[0]][2],layt[e[1]][2], None]
import plotly
plotly.tools.set_credentials_file(username='', api_key='')
#Enter your own username and API Keys.
# In[71]:
import chart_studio.plotly as py
from plotly.graph_objs import *
trace1=Scatter3d(x=Xe,
y=Ye,
z=Ze,
mode='lines',
line=Line(color='rgb(125,125,125)', width=1),
hoverinfo='none'
)
trace2=Scatter3d(x=Xn,
y=Yn,
z=Zn,
mode='markers',
name='actors',
marker=Marker(symbol='dot',
color='blue',
size=6,colorbar=ColorBar(
title='Colorbar'
),
colorscale='Viridis',
line=Line(color='rgb(158,18,130)', width=0.5)
),
text=labels,
hoverinfo='text'
)
axis=dict(showbackground=False,
showline=False,
zeroline=False,
showgrid=False,
showticklabels=False,
title=''
)
layout = Layout(
title="3D Visualization of the Facebook nodes",
width=1000,
height=1000,
showlegend=False,
scene=Scene(
xaxis=XAxis(axis),
yaxis=YAxis(axis),
zaxis=ZAxis(axis),
),
margin=Margin(
t=100
),
hovermode='closest',
annotations=Annotations([
Annotation(
showarrow=False,
text="Data source: <a href='http://bost.ocks.org/mike/miserables/miserables.json'>[1] miserables.json</a>",
xref='paper',
yref='paper',
x=0,
y=0.1,
xanchor='left',
yanchor='bottom',
font=Font(
size=14
)
)
]), )
data=Data([trace1, trace2])
fig=Figure(data=data, layout=layout)
py.iplot(fig)