{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n", "\n", "import plotly.plotly as py\n", "from plotly.graph_objs import *\n", "\n", "import networkx as nx\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "\n", "\n", "G=nx.random_geometric_graph(200,0.125)\n", "pos=nx.get_node_attributes(G,'pos')\n", "\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\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mfor\u001b[0m \u001b[0mnode\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0madjacencies\u001b[0m \u001b[1;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mG\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madjacency_list\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 4\u001b[0m \u001b[0mnode_trace\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'marker'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'color'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0madjacencies\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mnode_info\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'# of connections: '\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0madjacencies\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mAttributeError\u001b[0m: 'Graph' object has no attribute 'adjacency_list'" ] } ], "source": [ "\n", "\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", "\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fig = Figure(data=Data([edge_trace, node_trace]),\n", " layout=Layout(\n", " title='
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: https://plot.ly/ipython-notebooks/network-graphs/\",\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", "py.iplot(fig, filename='plotly-demo/networkx')" ] } ], "metadata": { "kernelspec": { "display_name": "Python [Root]", "language": "python", "name": "Python [Root]" }, "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.12" } }, "nbformat": 4, "nbformat_minor": 0 }