Created
October 21, 2015 17:46
-
-
Save cldougl/7797d7ad94816064cd05 to your computer and use it in GitHub Desktop.
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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "import plotly.plotly as py\n", | |
| "from plotly.graph_objs import *\n", | |
| "\n", | |
| "import networkx as nx\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "\n", | |
| "G=nx.random_geometric_graph(200,0.125)\n", | |
| "# position is stored as node attribute data for random_geometric_graph\n", | |
| "pos=nx.get_node_attributes(G,'pos')\n", | |
| "\n", | |
| "# find node near center (0.5,0.5)\n", | |
| "dmin=1\n", | |
| "ncenter=0\n", | |
| "for n in pos:\n", | |
| " x,y=pos[n]\n", | |
| " d=(x-0.5)**2+(y-0.5)**2\n", | |
| " if d<dmin:\n", | |
| " ncenter=n\n", | |
| " dmin=d" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# add the edges in as disconnected lines in a single trace\n", | |
| "edge_trace = Scatter(\n", | |
| " x=[], \n", | |
| " y=[], \n", | |
| " line=Line(width=0.5,color='#888'),\n", | |
| " hoverinfo='none',\n", | |
| " mode='lines')\n", | |
| "\n", | |
| "for edge in G.edges():\n", | |
| " x0, y0 = G.node[edge[0]]['pos']\n", | |
| " x1, y1 = G.node[edge[1]]['pos']\n", | |
| " edge_trace['x'] += [x0, x1, None]\n", | |
| " edge_trace['y'] += [y0, y1, None]\n", | |
| "\n", | |
| "# add the nodes in as a scatter\n", | |
| "node_trace = Scatter(\n", | |
| " x=[], \n", | |
| " y=[], \n", | |
| " text=[],\n", | |
| " mode='markers', \n", | |
| " hoverinfo='text',\n", | |
| " marker=Marker(\n", | |
| " showscale=True,\n", | |
| " # colorscale options\n", | |
| " # 'Greys' | 'Greens' | 'Bluered' | 'Hot' | 'Picnic' | 'Portland' |\n", | |
| " # Jet' | 'RdBu' | 'Blackbody' | 'Earth' | 'Electric' | 'YIOrRd' | 'YIGnBu'\n", | |
| " colorscale='YIGnBu',\n", | |
| " reversescale=True,\n", | |
| " color=[], \n", | |
| " size=8, \n", | |
| " colorbar=dict(\n", | |
| " thickness=15,\n", | |
| " title='Node Connections',\n", | |
| " xanchor='left',\n", | |
| " titleside='right'\n", | |
| " ),\n", | |
| " line=dict(width=2)))\n", | |
| "\n", | |
| "for node in G.nodes():\n", | |
| " x, y = G.node[node]['pos']\n", | |
| " node_trace['x'].append(x)\n", | |
| " node_trace['y'].append(y)\n", | |
| "\n", | |
| "# color the node points by the number of connections\n", | |
| "for node, adjacencies in enumerate(G.adjacency_list()):\n", | |
| " node_trace['marker']['color'].append(len(adjacencies))\n", | |
| " node_info = '# of connections: '+str(len(adjacencies))\n", | |
| " node_trace['text'].append(node_info)\n", | |
| " # could also size points by number of connections\n", | |
| " # node_trace['marker']['size'].append(len(adjacencies))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<iframe id=\"igraph\" scrolling=\"no\" style=\"border:none;\"seamless=\"seamless\" src=\"https://plot.ly/~chelsea_lyn/7803.embed\" height=\"650px\" width=\"650px\"></iframe>" | |
| ], | |
| "text/plain": [ | |
| "<plotly.tools.PlotlyDisplay object>" | |
| ] | |
| }, | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "# create a figure so we can customize a couple more things\n", | |
| "fig = Figure(data=Data([edge_trace, node_trace]),\n", | |
| " layout=Layout(\n", | |
| " title='<br>Network graph made with Python',\n", | |
| " titlefont=dict(size=16),\n", | |
| " showlegend=False, \n", | |
| " width=650,\n", | |
| " height=650,\n", | |
| " hovermode='closest',\n", | |
| " margin=dict(b=20,l=5,r=5,t=40),\n", | |
| " annotations=[ dict(\n", | |
| " text=\"Python code: <a href='https://plot.ly/ipython-notebooks/network-graphs/'> https://plot.ly/ipython-notebooks/network-graphs/</a>\",\n", | |
| " showarrow=False,\n", | |
| " xref=\"paper\", yref=\"paper\",\n", | |
| " x=0.005, y=-0.002 ) ],\n", | |
| " xaxis=XAxis(showgrid=False, zeroline=False, showticklabels=False),\n", | |
| " yaxis=YAxis(showgrid=False, zeroline=False, showticklabels=False)))\n", | |
| "\n", | |
| "# send the figure to Plotly and embed an iframe in this notebook\n", | |
| "py.iplot(fig, filename='networkx')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 2", | |
| "language": "python", | |
| "name": "python2" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 2 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython2", | |
| "version": "2.7.9" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment