Python/networkx graph magic

Basic graph representation function on top of networkx graph library.

import networkx as nx
def plot_graph(nodes, edges, labels=False, node_size=False,
node_color='r', arrows=False, alpha=0.1):
    """nodes: list of nodes. Used as labels if labels=True
edges: list of edges. Edges are tuples.
node_size: size or list of sizes
node_color: color or list of colors
    G = nx.DiGraph()
nx.draw(G, with_labels=labels, node_color=node_color,
node_size=node_size, arrows=arrows, alpha=alpha)

Example with a simple fictional dataset:

import pandas as pd
# Node data
Stations = pd.DataFrame([['Station1', 2500],['Station2',1210],
['Station3', 90],['Station4', 312],
['Station5', 11],['Station6', 190]],
columns=['Station', 'Traffic'])
# Edge data
routes = pd.DataFrame([['Station1', 'Station2'],
['Station1', 'Station4'],
['Station1', 'Station5'],
['Station2', 'Station3'],
['Station2', 'Station5'],
['Station2', 'Station6'],
['Station3', 'Station5'],
['Station4', 'Station1'],
['Station4', 'Station2'],
['Station4', 'Station6'],
['Station5', 'Station6']],
columns=['start', 'stop'])
edges=[tuple(row) for row in routes.values],

Example with an anonymized Facebook dataset (Stanford Large Network Dataset Collection); 4,039 nodes 88,233 edges:

data = pd.read_csv('facebook_combined.txt', sep='\t')
# At this stage edges are strings. We transform them to tuples:
edges = [tuple(x[0].split(' ')) for x in data.values]
# We use a set over a nested list comprehension to make a deduplicated list of nodes
nodes = set([item for edge in edges for item in edge])
plot_graph(nodes, edges, alpha=0.05)
Like what you read? Give Olivier Cruchant a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.