Created
August 25, 2017 11:12
-
-
Save ytakashina/81c24d91ef4060cbda812a9d75cadea1 to your computer and use it in GitHub Desktop.
plotly でネットワークを描画しようとした
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import plotly | |
import plotly.offline as py | |
import plotly.graph_objs as go | |
plotly.offline.init_notebook_mode(connected=True) | |
# pos = nx.spring_layout(graph) | |
pos = nx.circular_layout(graph) | |
Xv = [v[0] for v in pos.values()] | |
Yv = [v[1] for v in pos.values()] | |
Xed = np.array([[pos[edge[0]][0], pos[edge[1]][0]] for edge in graph.edges()]).flatten() | |
Yed = np.array([[pos[edge[0]][1], pos[edge[1]][1]] for edge in graph.edges()]).flatten() | |
labels = graph.nodes() | |
trace1=go.Scatter(x=Xed, | |
y=Yed, | |
mode='lines', | |
line=go.Line(width=0.5,color='#888'), | |
name='follow', | |
hoverinfo='none' | |
) | |
trace2=go.Scatter(x=Xv, | |
y=Yv, | |
mode='markers', | |
name='users', | |
marker=go.Marker(symbol='dot', color='red', size=20), | |
text=labels, | |
hoverinfo='text' | |
) | |
images = [{"source": graph.node[user_id]['image_url'], | |
"x": pos[user_id][0]/2.3 + 0.48, # hardcoding transformation | |
"y": pos[user_id][1]/2.3 + 0.55, # not good | |
"sizex": 0.1, | |
"sizey": 0.1, | |
} for user_id in graph.nodes()] | |
layout= go.Layout(images=images) | |
fig=go.Figure(data=[trace1, trace2], layout=layout) | |
py.iplot(fig) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment