In this tutorial, we're going to use the PyGraphistry python library to create and visualize our first graph, then move on to more advanced techniques like creating a graph from any CSV, modifying visualization properties, and analyzing data from within the Graphistry visual environment.
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.
links = pandas.read_csv('./lesmiserables.csv')
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:
g = graphistry.bind(source="source", destination="target")
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")
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
© Graphistry Inc 2017