Blog of Sara Jakša

Visualizing Network with Python and Networkx

Graph of Heroes characters interacting in the first episode

I have borrowed an O'Reilly's book Data Science from Scratch by Joel Grus from the library. The examples in the first chapter weren't interesting in their problem content, as I am not that interested in the social networks. But there was a nice visualization of the network and I got interested in learning first how to visualize networks.

I read the whole first chapter, and there is nothing. I read a chapter on visualization, and there are examples for scatter plots and histograms, but no networks. I ended up going to he chapter on analyzing networks, but there was nothing there either.

Now interested in the problem, I decided to hit the Google and try learn by myself how to visualize it. I took an episode of a series that I recently watched and decided to get the data by rewatching the first episode. I ended up using Networkx and matplotlib, simply because that was one of the hits on Google.

The picture above is the visualization that I ended up creating with the code that is written down below.

    import networkx as nx
    import matplotlib.pyplot as pyplt
    from collections import defaultdict

    # Data used to create a graph. Users is a list of dictionaries of all the named people that appear on Heroes season 1 episode 1. Meetings is the list of tuples that indicate which people had interaction with one another
    users = [{"id": 0, "name": "Charles Deveaux"},
    {"id": 1, "name": "Peter Petrelli"},
    {"id": 2, "name": "Nathan Petrelli"},
    {"id": 3, "name": "Simone Deveaux"},
    {"id": 4, "name": "Mohinder Suresh"},
    {"id": 5, "name": "Nirad"},
    {"id": 6, "name": "Noah Bennet"},
    {"id": 7, "name": "Niki Sanders"},
    {"id": 8, "name": "Ando Masahashi"},
    {"id": 9, "name": "Micah Sanders"},
    {"id": 10, "name": "Claire Bennet"},
    {"id": 11, "name": "Daniel Linderman"},
    {"id": 12, "name": "Angela Petrelli"},
    {"id": 13, "name": "Zach"},
    {"id": 14, "name": "Hiro Nakamura"},
    {"id": 15, "name": "Tina"},
    {"id": 16, "name": "Isaac Mendez"},
    {"id": 17, "name": "Sandra Bennet"},
    {"id": 18, "name": "Lyle Bennet"}]

    meetings = [(2,12), (1,2), (1,12), (6,10), (6,17), (10,17), (1,16), (3,16), (1,3),(1,4), (1,0), (1,0), (1,3),(0,3),(1,0),(1,3),(0,3),(6,4),(1,2),(1,3),(10,17),(8,14),(8,14),(8,14),(10,13),(1,4),(1,2),(3,16),(12,1),(7,9),(8,14),(13,10),(1,2),(13,10),(7,9),(4, 6),(10, 18),(10,17),(17,18),(15,9),(15,7),(9,7),(2,11),(4,5),]

    #Creates a new graph, puts all people there as a node.
    G=nx.Graph()
    for user in users:
        G.add_node(user["name"])

    #For each pair of people it calculates the weight of the edge based on how many interactions did they have
    weight_meetings = defaultdict(int)
    for meeting in meetings:
        if meeting[0] < meeting[1]:
            meeting = (meeting[1], meeting[0])
        weight_meetings[(meeting)] += 1

    #Puts pairs of people that had interaction in the episode in a graph as an edge.
    for meeting, weight in weight_meetings.items():
        G.add_edge(users[meeting[0]]["name"], users[meeting[1]]["name"], weight=weight)

    #Takes the edges from the graph and put the weights of each edge in a list
    edges = G.edges()
    weights = [G[u][v]['weight'] for u,v in edges]

    #Draws the graph
    pos=nx.spring_layout(G)
    nx.draw(G, pos=pos, node_size=100, node_color = "yellow", edge_color = "orange", with_labels=True, font_size=12, font_color = "blue", width=weights)

    #Shows the graph
    pyplt.show()