Section 2. Visualizing Existing Graphs




Let's visualize relationships between the characters in Les Misérables from data already in a graph format. For this example, we'll choose Pandas to wrangle data and IGraph to run a community detection algorithm. You can view and download the IPython notebook containing this example.


import pandas
links = pandas.read_csv('./lesmiserables.csv')


Quick Visualization


If you already have graph-like data, use this step. Otherwise, try the hypergraph transform on the next tutorial page.

PyGraphistry can plot graphs directly from Pandas dataframes, IGraph graphs, or NetworkX graphs. Calling plot uploads the data to our visualization servers and return an URL to an embeddable webpage containing the visualization.

To declare the graph structure in the data tables, we create a binding with source and destination bound to the columns indicating the start and end nodes of each edges:


import graphistry
graphistry.register(key='YOUR_API_KEY_HERE')

g = graphistry.bind(source="source", destination="target")
g(links)


Adding Labels


Let's add labels to edges in order to show how many times each pair of characters met. We create a new column called label in edge table links that contains the text of the label and we create an extended binding with edge_label bound to the column.


links["label"] = links.value.map(lambda v: "#Meetings: %d" % v)
g = g.bind(edge_label="label")
g.plot(links)


Controlling Node Size and Color


Let's size nodes based on their PageRank score and color them using their community. IGraph already has these algorithms implemented for us. If IGraph is not already installed, fetch it with pip install python-igraph. Warning: pip install igraph will install the wrong package!

We start by converting our edge dateframe into an IGraph. The Graphistry instance can do the conversion for us using the source and destination bindings. Then we create two new node attributes (pagerank & community).


ig = g.pandas2igraph(links)
ig.vs['pagerank'] = ig.pagerank()
ig.vs['community'] = ig.community_infomap().membership

g.bind(point_color='community', point_size='pagerank').plot(ig)


Example Notebook:





Next: Graphing any CSV - Hypergraphs and Your Data