Network Graph with AT&T data using Plotly
Tags: Data Visualization, Plotly, ATT, Plot, Network

I recently made a network graph using plotly on AT&T data and it turned out to be beautiful so I thought why not share it with everyone.

This will be a detailed tutorial on how to make a network graph. In addition to plotly, we will also be using networkx to construct the graph. You can download the dataset from kaggle.

Network graph helps in visualizing connections between different nodes. This, in turn, would help us with network analysis. So without further ado lets discuss our data and see how we can plot it.

When we see the data there are two columns with source IP and destination IP which represent our edges. And each of these unique IP addresses is a node.

Head of the dataset

We use networkx to build our graph and include our nodes and edges in the graph and then pass on the nodes and edges to plotly to plot.

A = list(network_df["source_ip"].unique())

B = list(network_df["destination_ip"].unique())

node_list = list(set(A+B))

G = nx.Graph()

for i in node_list:

G.add_node(i)

In the above for loop, we add all the nodes into the graph using the add_node method. Now we add edges to the graph.

for i,j in network_df.iterrows():

G.add_edges_from([(j["source_ip"],j["destination_ip"])])

We now assign some position to each node for plotting. We can do so using a very convenient method called spring_layout in networkx.

pos = nx.spring_layout(G, k=0.5, iterations=50)

for n, p in pos.items():

G.node[n]['pos'] = p

Now that our graph is set, we are ready to plot using plotly.

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]) node_trace = go.Scatter(

x=[],

y=[],

text=[],

mode='markers',

hoverinfo='text',

marker=dict(

showscale=True,

colorscale='RdBu',

reversescale=True,

color=[],

size=15,

colorbar=dict(

thickness=10,

title='Node Connections',

xanchor='left',

titleside='right'

),

line=dict(width=0))) for node in G.nodes():

x, y = G.node[node]['pos']

node_trace['x'] += tuple([x])

node_trace['y'] += tuple([y])

In the code above we create go objects of plotly and assign the data to the objects. Now we assign annotations to the nodes and edges.

for node, adjacencies in enumerate(G.adjacency()):

node_trace['marker']['color']+=tuple([len(adjacencies[1])])

node_info = adjacencies[0] +' # of connections: '+str(len(adjacencies[1]))

node_trace['text']+=tuple([node_info])

Now we plot go objects created above node_trace and edge_trace using Figure.

fig = go.Figure(data=[edge_trace, node_trace],

layout=go.Layout(

title='<br>AT&T network connections',

titlefont=dict(size=16),

showlegend=False,

hovermode='closest',

margin=dict(b=20,l=5,r=5,t=40),

annotations=[ dict(

text="No. of connections",

showarrow=False,

xref="paper", yref="paper") ],

xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),

yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)))

iplot(fig)

The output is an interactive network graph which could be saved as a png, HTML and many other forms. For the sake of using the interactive nature, I saved it as an HTML and uploaded to plot.ly. Check out the below link which is the output of the above code.

So that's it! We created a network graph. The code for it is available on my Kaggle and Github. Let me know what you think below. You can always hit me up on my mail ID saianand0427@gmail.com. Clap and share! See you in my next blog!