{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Quickstart: Graphing Les Misérables\n", "\n", "This simple example from our [quickstart walkthrough](http://graphistry.github.io/pygraphistry/index.html#quickstart-graph-les-misérables) introduces the basics of PyGraphistry. We also have more advanced tutorials avaiable.\n", "\n", "You can [download this notebook](https://github.com/graphistry/pygraphistry/tree/master/demos) to run it locally." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pandas\n", "import graphistry\n", "\n", "graphistry.register(key='Email pygraphistry@graphistry.com to get your API key')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Parse CSV using Pandas\n", "links = pandas.read_csv('data/lesmiserables.csv')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sourcetargetvalue
0NapoleonMyriel1
1Mlle.BaptistineMyriel8
2Mme.MagloireMyriel10
\n", "
" ], "text/plain": [ " source target value\n", "0 Napoleon Myriel 1\n", "1 Mlle.Baptistine Myriel 8\n", "2 Mme.Magloire Myriel 10" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Let's have a peak at our data by printing the first three rows\n", "links[:3]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Plot graph using the source/target columns as source/destination of edges\n", "plotter = graphistry.bind(source='source', destination='target')\n", "plotter.plot(links)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# New graph adding the number of encounters to edge labels.\n", "links['label'] = links.value.map(lambda v: 'Num. Encounters: %d' % v)\n", "plotter = plotter.bind(edge_label='label')\n", "plotter.plot(links)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Controling Node Size and Color\n", "We are going to use Igraph to color nodes by community and size them using pagerank. To install igraph, use `pip install python-igraph`" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "IGRAPH D--- 77 254 -- \n", "+ attr: __nodeid__ (v), label (e), value (e)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "WARNING: \"node\" is unbound, automatically binding it to \"__nodeid__\".\n" ] } ], "source": [ "# Convert our graph from Pandas to Igraph\n", "import igraph\n", "ig = plotter.pandas2igraph(links)\n", "igraph.summary(ig)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "IGRAPH D--- 77 254 -- \n", "+ attr: __nodeid__ (v), community (v), pagerank (v), label (e), value (e)\n" ] } ], "source": [ "# We create two node attributes for pagerank and community\n", "ig.vs['pagerank'] = ig.pagerank()\n", "ig.vs['community'] = ig.community_infomap().membership \n", "igraph.summary(ig)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The plotter can plot Igraph directly\n", "plotter.bind(point_color='community', point_size='pagerank').plot(ig)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }