{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ " ## Barabasi-Erdos Graph ##" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A Barabasi-Albert graph is a random graph [https://en.wikipedia.org/wiki/Barab%C3%A1si%E2%80%93Albert_model](https://en.wikipedia.org/wiki/Barab%C3%A1si%E2%80%93Albert_model)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import plotly.plotly as py\n", "from plotly.graph_objs import *\n", "import plotly.tools as tls \n", "import networkx as nx" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "nr_nodes=150\n", "G=nx.barabasi_albert_graph(nr_nodes, 5)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "pos=nx.random_layout(G, dim= 2)# get node positions" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "clusteringC=[nx.clustering(G,v) for v in G.nodes()]#list of clustering coefficients\n", "degree=[G.degree(v) for v in G.nodes()] # list of node degrees\n", "labels=['Node deg: '+ str(G.degree(v))+'
Clust coeff: '\\\n", " +'{:.5f}'.format(nx.clustering(G,v)) for v in G.nodes()]#labels for nodes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define the lists of node coordinates, Xn, Yn, and the lists of coordinates Xe, Ye, for ends of edges:" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": true }, "outputs": [], "source": [ "L=len(pos)\n", "X=[]\n", "Y=[]\n", "Xn=[pos[k][0] for k in range(L)]\n", "Yn=[pos[k][1] for k in range(L)]\n", "Xe=[]\n", "Ye=[]\n", "for edge in G.edges():\n", " Xe+=[pos[edge[0]][0],pos[edge[1]][0], None]\n", " Ye+=[pos[edge[0]][1],pos[edge[1]][1], None] " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We define two Plotly plots for the same Barabasi-Albert graph. In the first one the node colors are assigned according to their clustering coefficients\n", "and in the second one, according to the node degree." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Custom colorscale to plot the nodes in one of the two subplots:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "nyrev_cs=[[0.0, '#FFFFE0'],\n", " [0.06666666666666667, '#FFEEC1'],\n", " [0.13333333333333333, '#FFDEA7'],\n", " [0.2, '#FFCB91'],\n", " [0.26666666666666666, '#FFB880'],\n", " [0.3333333333333333, '#FFA474'],\n", " [0.4, '#FE906A'],\n", " [0.4666666666666667, '#F87D64'],\n", " [0.5333333333333333, '#F06A5E'],\n", " [0.6, '#E75758'],\n", " [0.6666666666666666, '#DB4551'],\n", " [0.7333333333333333, '#CF3447'],\n", " [0.8, '#C0223B'],\n", " [0.8666666666666667, '#B0122C'],\n", " [0.9333333333333333, '#9E051B'],\n", " [1.0, '#8B0000']]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define Plotly Scatter objects for edges (trace 1) and nodes (trace2 and trace3):" ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": false }, "outputs": [], "source": [ "trace1=Scatter(x=Xe,\n", " y=Ye,\n", " mode='lines',\n", " line=Line(color='rgb(210,210,210)', width=0.5),\n", " hoverinfo='none'\n", " )\n", "trace2=Scatter(x=Xn,\n", " y=Yn,\n", " mode='markers',\n", " name='B-A',\n", " marker=Marker(symbol='dot',\n", " size=10, \n", " color=clusteringC, \n", " colorscale=nyrev_cs, #custom colorscale\n", " #colorbar={'title':'Clustering coeff', 'titleside':'top'},\n", " line=Line(color='rgb(50,50,50)', width=0.5)\n", " ),\n", " text=labels,\n", " hoverinfo='text'\n", " )\n", "trace3=Scatter(x=Xn,\n", " y=Yn,\n", " mode='markers',\n", " name='B-A',\n", " marker=Marker(symbol='dot',\n", " size=10, \n", " color=degree, \n", " colorscale='YIGnBu', #Plotly colorscale\n", " reversescale=True,\n", " #colorbar={'title':'Node degree', 'titleside':'top'},\n", " line=Line(color='rgb(50,50,50)', width=0.5)\n", " ),\n", " text=labels,\n", " hoverinfo='text'\n", " )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Subplot titles:" ] }, { "cell_type": "code", "execution_count": 68, "metadata": { "collapsed": true }, "outputs": [], "source": [ "titles=('Colors assigned according to the
node clustering coefficient', \n", " 'Colors assigned according to the
node degree')" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This is the format of your plot grid:\n", "[ (1,1) x1,y1 ] [ (1,2) x2,y2 ]\n", "\n" ] } ], "source": [ "figure = tls.make_subplots(rows=1, cols=2, subplot_titles=titles,\n", " horizontal_spacing=0.025, \n", " print_grid=True)" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "collapsed": true }, "outputs": [], "source": [ "axis=dict(showline=False, # hide axis line, grid, ticklabels and title\n", " zeroline=False,\n", " showgrid=False,\n", " showticklabels=False,\n", " title='' \n", " )\n", "def make_XAxis():\n", " xaxis = XAxis() \n", " xaxis.update(axis) \n", " return xaxis\n", "def make_YAxis():\n", " yaxis = YAxis() \n", " yaxis.update(axis) \n", " return yaxis" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "collapsed": true }, "outputs": [], "source": [ "width=1000\n", "height=500\n", "\n", "figure['layout'].update(title= 'Barabasi-Albert Graph of parameters (m,n)=(150, 5)', \n", " font= Font(size=14),\n", " showlegend=False,\n", " autosize=False,\n", " width=width,\n", " height=height,\n", " margin=Margin(\n", " l=40,\n", " r=40,\n", " b=85,\n", " t=120,\n", " ),\n", " hovermode='closest', \n", " )\n" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "collapsed": false }, "outputs": [], "source": [ "figure.append_trace(trace1, 1, 1)\n", "figure.append_trace(trace2, 1, 1)\n", "figure.append_trace(trace1, 1, 2)\n", "figure.append_trace(trace3, 1, 2)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": { "collapsed": true }, "outputs": [], "source": [ "for s in [1,2]: \n", " figure['layout'].update({'xaxis{}'.format(s): make_XAxis()})# set xaxis style\n", " figure['layout'].update({'yaxis{}'.format(s): make_YAxis()})# set yaxis style" ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "u'https://plot.ly/~empet/8024'" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "py.sign_in('empet', '')\n", "py.iplot(figure, filename='Barabasi-Albert-Graph')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Click on the image below to access the interactive plots:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", " \n", "
\n", "\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.core.display import HTML\n", "def css_styling():\n", " styles = open(\"./custom.css\", \"r\").read()\n", " return HTML(styles)\n", "css_styling()" ] } ], "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.10" } }, "nbformat": 4, "nbformat_minor": 0 }