-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3d_chart.py
More file actions
131 lines (105 loc) · 3.16 KB
/
3d_chart.py
File metadata and controls
131 lines (105 loc) · 3.16 KB
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
import json
import urllib.request
import igraph as ig
import streamlit as st
# Read graph data from a json file:
data = []
req = urllib.request.urlopen("https://raw.githubusercontent.com/plotly/datasets/master/miserables.json").read()
data = json.loads(req)
print(data.keys())
# Get the number of nodes:
N=len(data['nodes'])
print(N)
# Define the list of edges and the Graph object from Edges:
L=len(data['links'])
Edges=[(data['links'][k]['source'], data['links'][k]['target']) for k in range(L)]
G=ig.Graph(Edges, directed=False)
print(type(G))
# Extract the node attributes, 'group', and 'name':
print(data['nodes'][0])
labels=[]
group=[]
for node in data['nodes']:
labels.append(node['name'])
group.append(node['group'])
st.write(labels)
st.write(group)
# Get the node positions, set by the Kamada-Kawai layout for 3D graphs:
layt=G.layout('kk', dim=3)
st.write(layt[5])
# Set data for the Plotly plot of the graph:
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 chart_studio.plotly as py
# import chart_studio as go
import plotly as py
from plotly import graph_objects as go
trace1=go.Scatter3d(x=Xe,
y=Ye,
z=Ze,
mode='lines',
line=dict(color='rgb(125,125,125)', width=1),
hoverinfo='none'
)
trace2=go.Scatter3d(x=Xn,
y=Yn,
z=Zn,
mode='markers',
name='actors',
marker=dict(symbol='circle',
size=6,
color=group,
colorscale='Viridis',
line=dict(color='rgb(50,50,50)', width=0.5)
),
text=labels,
hoverinfo='text'
)
axis=dict(showbackground=False,
showline=False,
zeroline=False,
showgrid=False,
showticklabels=False,
title=''
)
layout = go.Layout(
title="Network of coappearances of characters in Victor Hugo's novel<br> Les Miserables (3D visualization)",
width=1000,
height=1000,
showlegend=False,
scene=dict(
xaxis=dict(axis),
yaxis=dict(axis),
zaxis=dict(axis),
),
margin=dict(
t=100
),
hovermode='closest',
annotations=[
dict(
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=dict(
size=14
)
)
], )
data=[trace1, trace2]
fig=go.Figure(data=data, layout=layout)
# py.offline.iplot(fig, filename='Les-Miserables')
st.plotly_chart(fig)