name: inverse layout: true class: center, middle, inverse --- # Python Programming ## for Humanities and Social Science Students ### Making Networks --- layout: false # "Rules" of the class **Ask questions** as we go along. - **Remember**: There's always more than one way to do things! - The goal here is not to be perfect but to get you up and running quickly. - Remember keep your own data in mind as we go through these slides. - Much of class time will be dedicated to working through your own research.
--- name: inverse layout: true class: center, middle, inverse --- # Ready? # Part 1: Making a network --- name: how layout: false .left-column[ # We'll use Python to... ] .right-column[ - Create a network - Visualize a network - Export a network (to Gephi) - Analyze a network
] --- name: how layout: false .left-column[ # Python ## .orange[Networkx] ] .right-column[ - We'll use the **networkx** library - But there are many others - First, how to download a python library "package" --- ##Using **pip** to download python libraries If you don't have **pip** use **easy_install** to install it! Now, assuming pip is installed, run the following: ```remark-code pip install networkx # OR if you're on a mac sudu -H pip install networkx ``` You should see the packages being installed on your computer! ] --- #Let's look at a network! - Easiest way: load a built-in network. - Famous graph of relationships between wealthy Medici families ###Citations - Breiger R. and Pattison P. (1986). Cumulated social roles: The duality of persons and their algebras. Social Networks, 8, 215-256. - Kent D. (1978). The rise of the Medici: Faction in Florence, 1426-1434. Oxford: Oxford University Press. ```remark-code import networkx as nx g = nx.florentine_families_graph() print "There are %d nodes" % len( g.nodes() ) >>> There are 15 nodes ``` --- # Now let's explore a little more. ### Documentation is here: https://networkx.readthedocs.io ```remark-code import networkx as nx g = nx.florentine_families_graph() # get a graph object for n in g.nodes(): # returns a list of nodes in print n # print the node id >>> Strozzi Tornabuoni Medici Albizzi Guadagni Pazzi Acciaiuoli Bischeri Peruzzi Ginori Salviati Castellani Lamberteschi Ridolfi Barbadori ``` --- name: how layout: false # Now let's visualize this network ```remark-code import networkx as nx import matplotlib.pyplot as plt # MatPlot is a very powerful and complex lib! g = nx.florentine_families_graph() graph_pos = nx.shell_layout(g) # Define how to place the nodes # Set the colours and text nx.draw_networkx_nodes(g, graph_pos, node_size=1000, node_color='blue', alpha=0.3) nx.draw_networkx_edges(g, graph_pos) nx.draw_networkx_labels(g, graph_pos, font_size=12, font_family='sans-serif') # Show graph plt.show() ``` Results? --- name: how layout: false .left-column[ ### Medici Family Relationships ] .right-column[
.blue[Question: Which family is most important? ] ] --- name: how layout: false .left-column[ ## NetworkX ### .orange[Exporting graphs] ] .right-column[ - Networkx and matplotlib are great tools for visualizing small networks quickly. - Larger graphs need dedicated tools - **Gephi** is the best known graph visualization tool. - http://gephi.org
] --- # NetworkX Exporting graphs - Export Medici family data - Gephi reads gexf files (XML text file) ```remark-code nx.write_gexf( g, "medici.gexf" ) ``` The GEXF looks like
--- #NetworkX and Gephi - Same graph file visualized with **Gephi** - ForceAtlas layout - Size of nodes is degree (number of edges)
--- #NetworkX and Gephi Lessons: Visualization helps to tell the story - Gephi is much easier to work with - Much more clear which family is more important
.blue[Question: Which family is most important? ] --- # Analyzing networks... --- --- ### Questions? ---