Created
October 12, 2017 15:39
-
-
Save brynpickering/b3257ab711c27ad878bca8b1cc8ba5f1 to your computer and use it in GitHub Desktop.
Plotly network implementation
This file contains 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
# direct copy from https://stackoverflow.com/questions/46037897/line-hover-text-in-plotly with some lines changed (originals in comments) | |
import plotly.offline as py | |
from plotly.graph_objs import * | |
py.init_notebook_mode(connected=True) | |
import networkx as nx | |
G=nx.random_geometric_graph(200,0.125) | |
pos=nx.get_node_attributes(G,'pos') | |
dmin=1 | |
ncenter=0 | |
for n in pos: | |
x,y=pos[n] | |
d=(x-0.5)**2+(y-0.5)**2 | |
if d<dmin: | |
ncenter=n | |
dmin=d | |
p=nx.single_source_shortest_path_length(G,ncenter) | |
trace3_list = [] | |
middle_node_trace = Scatter( | |
x=[], | |
y=[], | |
text=[], | |
mode='markers', | |
hoverinfo='text', | |
marker=Marker( | |
opacity=0 | |
) | |
) | |
for edge in G.edges(data=True): | |
trace3=Scatter( | |
x=[], | |
y=[], | |
mode='lines', | |
# line=Line(color='rgb(210,210,210)', width=edge[2]['weight']), | |
line=Line(color='rgb(210,210,210)', width=1), | |
hoverinfo='none' | |
) | |
x0, y0 = G.node[edge[0]]['pos'] | |
x1, y1 = G.node[edge[1]]['pos'] | |
trace3['x'] += [x0, x1, None] | |
trace3['y'] += [y0, y1, None] | |
trace3_list.append(trace3) | |
middle_node_trace['x'].append((x0+x1)/2) | |
middle_node_trace['y'].append((y0+y1)/2) | |
# middle_node_trace['text'].append(str(edge[2]['weight'])) | |
middle_node_trace['text'].append(str(1)) | |
node_trace = Scatter( | |
x=[], | |
y=[], | |
text=[], | |
mode='markers', | |
hoverinfo='text', | |
marker=Marker( | |
showscale=True, | |
# colorscale options | |
# 'Greys' | 'Greens' | 'Bluered' | 'Hot' | 'Picnic' | 'Portland' | | |
# Jet' | 'RdBu' | 'Blackbody' | 'Earth' | 'Electric' | 'YIOrRd' | 'YIGnBu' | |
colorscale='YIGnBu', | |
reversescale=True, | |
color=[], | |
size=10, | |
colorbar=dict( | |
thickness=15, | |
title='Node Connections', | |
xanchor='left', | |
titleside='right' | |
), | |
line=dict(width=2))) | |
for node in G.nodes(): | |
x, y = G.node[node]['pos'] | |
node_trace['x'].append(x) | |
node_trace['y'].append(y) | |
for node, adjacencies in enumerate(G.adjacency()): | |
node_trace['marker']['color'].append(len(adjacencies)) | |
node_info = '# of connections: '+str(len(adjacencies)) | |
node_trace['text'].append(node_info) | |
fig = Figure(data=Data([*trace3_list, middle_node_trace, node_trace]), | |
layout=Layout( | |
title='<br>Network graph made with Python', | |
titlefont=dict(size=16), | |
showlegend=False, | |
hovermode='closest', | |
margin=dict(b=20,l=5,r=5,t=40), | |
annotations=[ dict( | |
text="Python code: <a href='https://plot.ly/ipython-notebooks/network-graphs/'> https://plot.ly/ipython-notebooks/network-graphs/</a>", | |
showarrow=False, | |
xref="paper", yref="paper", | |
x=0.005, y=-0.002 ) ], | |
xaxis=XAxis(showgrid=False, zeroline=False, showticklabels=False), | |
yaxis=YAxis(showgrid=False, zeroline=False, showticklabels=False))) | |
py.iplot(fig, filename='networkx') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the code . I am Getting Error "TypeError: can only concatenate tuple (not "list") to tuple"