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