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.
import pandas as pd
%pylab inline
import networkx as nx
import numpy as np
We'll use the gml file written by NetworkX in Part 1.
g = nx.read_gml("dolphins.gml")
g
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.
import plotly.graph_objs as go
import plotly.plotly as py
We set a node attribute, "pos", as the positions the nodes would take in a circular layout.
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.
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.
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])
for node in g.nodes():
x, y = g.node[node]['pos']
node_trace['x'] += tuple([x])
node_trace['y'] += tuple([y])
for node, adjacencies in enumerate(g.adjacency()):
node_trace['marker']['color']+=tuple([len(adjacencies[1])])
node_info = '# of connections: '+str(len(adjacencies[1]))
node_trace['text']+=tuple([node_info])
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.
plotly.tools.set_credentials_file(username="user", api_key="")
fig = go.Figure(data=[edge_trace, node_trace],
layout=go.Layout(
title='<br>Network graph made with Python',
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')