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
Populating the interactive namespace from numpy and matplotlib
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")
<networkx.classes.graph.Graph at 0x1a2500d208>
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]['pos'] x1, y1 = g.node[edge]['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)]) node_info = '# of connections: '+str(len(adjacencies)) node_trace['text']+=tuple([node_info])
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.
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')
/Users/gbang/anaconda3/lib/python3.6/site-packages/IPython/core/display.py:689: UserWarning: Consider using IPython.display.IFrame instead