dolphins-part2

# Dolphin Society Networks in Doubtful Sound, NZ, part 2¶

Earlier, I posted an entry on dolphin society networks and some simple network drawings you can do with just NetworkX. Nowadays of course a lot of new fancy visualizations are available when you interface NetworkX with more modern plotting libraries, so we'll be taking a look at a few in the next parts of this series. Today's is 2-D plotting in plotly, and much of the code here comes from plotly's own well-written docs.

In [1]:
import pandas as pd

In [2]:
%pylab inline

Populating the interactive namespace from numpy and matplotlib

In [3]:
import networkx as nx
import numpy as np


We'll use the gml file written by NetworkX in Part 1.

In [73]:
g = nx.read_gml("dolphins.gml")

In [74]:
g

Out[74]:
<networkx.classes.graph.Graph at 0x1a2500d208>

# 2-D plotting in plotly¶

2-D plotting in plotly involves defining a node trace, and an edge trace to let plotly know where the nodes should go in x,y space.

In [96]:
import plotly.graph_objs as go

In [105]:
import plotly.plotly as py


We set a node attribute, "pos", as the positions the nodes would take in a circular layout.

In [97]:
pos = nx.circular_layout(g)
nx.set_node_attributes(g, pos, 'pos')
#nx.get_node_attributes(g, "pos")


Here we use the plotly graph_objs.Scatter object to create a node_trace object, setting the sizes and colors of the nodes.

In [98]:
node_trace = go.Scatter(
x=[],
y=[],
text=[],
mode='markers',
hoverinfo='text',
marker=dict(
showscale=True,
# colorscale options
#'Greys' | 'YlGnBu' | 'Greens' | 'YlOrRd' | 'Bluered' | 'RdBu' |
#'Reds' | 'Blues' | 'Picnic' | 'Rainbow' | 'Portland' | 'Jet' |
#'Hot' | 'Blackbody' | 'Earth' | 'Electric' | 'Viridis' |
colorscale='YlGnBu',
reversescale=True,
color=[],
size=10,
colorbar=dict(
thickness=15,
title='Node Connections',
xanchor='left',
titleside='right'
),
line=dict(width=2)))


We set the edge traces.

In [103]:
edge_trace = go.Scatter(
x=[],
y=[],
line=dict(width=0.5,color='#888'),
hoverinfo='none',
mode='lines')

for edge in g.edges():
x0, y0 = g.node[edge[0]]['pos']
x1, y1 = g.node[edge[1]]['pos']
edge_trace['x'] += tuple([x0, x1, None])
edge_trace['y'] += tuple([y0, y1, None])

In [99]:
for node in g.nodes():
x, y = g.node[node]['pos']
node_trace['x'] += tuple([x])
node_trace['y'] += tuple([y])

In [100]:
for node, adjacencies in enumerate(g.adjacency()):
node_info = '# of connections: '+str(len(adjacencies[1]))
node_trace['text']+=tuple([node_info])

In [107]:
import plotly


Plotly requires an API key for most applications so that is provided in the cell below; if you'd like to rerun this notebook, you would need to supply your own API key.

In [112]:
plotly.tools.set_credentials_file(username="user", api_key="")

In [113]:
fig = go.Figure(data=[edge_trace, node_trace],
layout=go.Layout(
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=dict(showgrid=False, zeroline=False, showticklabels=False),
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)))

py.iplot(fig, filename='networkx')

/Users/gbang/anaconda3/lib/python3.6/site-packages/IPython/core/display.py:689: UserWarning: