You are on page 1of 9

Graph Analytics for Python

Developers
Lecture 2

Version 1.0

0
Table of Contents

Table of Contents

Importing and exporting graphs 1


Graph generators 1

Visualization using Matplotlib 2


Graph layouts 3

Visualization using GraphViz 4


Attributes 4
Graph Attributes 4
Vertex Attributes 4
Edge Attributes 4
Graphs 4
Vertices 5
Edges 5

Other visualization tools 6


D3.js 6
Cytoscape 6
Gephi 7
Importing and exporting graphs
NetworkX graphs can be easily exported in several formats as well as imported from the
same. These formats include:
● Adjacency List ● JSON
● Multiline Adjacency List ● LEDA
● Edge List ● YAML
● GEXF ● SparseGraph6
● GML ● Pajek
● Pickle ● GIS Shapefile
● GraphML

The complete list is available in the reference guide.

Graph generators
Graph generators such as binomial_graph() and erdos_renyi_graph() are provided in the
graph generators subpackage and are used to create predefined network structures. Some
of the most often used generators are:
● balanced_tree()
● complete_graph()
● complete_multipartite_graph()
● cycle_graph()
● path_graph()
● star_graph()
● karate_club_graph()
● les_miserables_graph()

Karate club network graph

Visualization using Matplotlib

1
NetworkX is not a graph visualizing package but basic drawing with Matplotlib is included
in the software package. Matplotlib is a comprehensive library for creating static,
animated, and interactive visualizations in Python.

The main function for drawing graphs using NetworkX is the draw_networkx() method or
the simplified draw() method. The draw_networkx() method contains options for node
positions, labeling, titles, and many other drawing features. The draw() method is mainly
for simple drawing without labels or axes.

Matplotlib graph visualization

Check the available drawing options in the reference guide:


● draw(): Reference guide
● draw_networkx(): Reference guide

Some other notable methods for drawing graph constructs include:


● draw_networkx_nodes(): Reference guide
This draws only the nodes of the graph G.
● draw_networkx_edges(): Reference guide
This draws only the edges of the graph G.
● draw_networkx_labels(): Reference guide
Draw node labels on the graph G.
● draw_networkx_edge_labels(): Reference guide
Draw edge labels.

2
Graph layouts
Graphs can be drawn with a specific layout using the following methods:
● draw_circular(): Reference guide
Draw the graph G with a circular layout.
● draw_kamada_kawai(): Reference guide
Draw the graph G with a Kamada-Kawai force-directed layout.
● draw_planar(): Reference guide
Draw a planar networkx graph with planar layout.
● draw_random(): Reference guide
Draw the graph G with a random layout.
● draw_spectral(): Reference guide
Draw the graph G with a spectral 2D layout.
● draw_spring(): Reference guide
Draw the graph G with a spring layout.
● draw_shell(): Reference guide
Draw networkx graph with shell layout.

You can also specify a layout in the draw() and draw_networkx() methods. Some of the
available layouts are bipartite_layout, circular_layout, kamada_kawai_layout, planar_layout,
and random_layout. Check out the complete list in the reference guide.

NetworkX layouts

3
Visualization using GraphViz
Graphs and Vertices (Called Nodes in the GraphViz notation) may have a large number of
attributes that affect both the graph's actual layout, and details like colors, labels, and line
types. I cover a few of the attributes below but for a full reference see the GraphViz
Attribute Index.

Attributes
Graph Attributes
● label="My Graph"; Label a graph itself.
● rankdir=LR; Lay the graph out from Left to Right, instead of Top to Bottom.
● {rank=same; a, b, c } Group nodes together at the same level of a graph.
● splines="line"; Force edges to be straight, no curves or angles.
● K=0.6; Used to influence the 'spring' used in the layout, Can be used to push
nodes. further apart, which is especially useful for twopi and sfdp layouts.

Vertex Attributes
● [label="Some Label"] Labels the Vertex.
● [color="red"] Colors the Vertex.
● [fillcolor="blue"] Fills the Vertex with the specified color.

Edge Attributes
● [label="Some Label"] Labels the Edge (Useful for Weights).
● [color="red"] Colors the Vertex (Useful for Paths).
● [penwidth=2.0] Adjusts the thickness of the edge line, Very useful for Paths.

Edges may also have a weight attribute, defined as [weight=0.5] for example, but note
that this doesn't display the weight directly, It instead acts as a hint to the graph layout to
give this edge a more direct routing.

Graphs
Graphs are defined as either a graph or a digraph using fairly standard syntax, similar to
an edge list.

graph { node1 -- node2; node3 -- node2; }


digraph { node1 -> node2; node3 -> node2; }

Vertices

4
Vertices are defined with a simple plaintext label, A, B, C, Test, Vertice1,
some_vertex , etc. If you need a more complicated label you can declare the vertex first,
before defining an edge, and give it a label attribute, for example:
digraph { someVertex[label="A Complicated Label"]; someVertex ->
node2; node2 -> node3; }

Edges
For the most part, the only concern with edges is their color/thickness, and Label, as the
rest is handled automatically by the graph/digraph definition. To paint an edge apply the
color attribute to the graph definition as follows
digraph { node1 -> node2[color="red"]; }
To Display a weight, we give the vertex itself a label, similar to how we label nodes
digraph { node1 -> node2[label="0.2"]; node2 -> node3[label="0.2"];
}
We can actually apply any label we like to edge in this way digraph { node1 ->
node2[label="edge1"]; node2 -> node3[label="edge2"]; }
You may combine these attributes in any way you please
digraph { node1[label="Some Complicated Label"]; node1 ->
node2[label="An Edge",color=red]; node2 -> node3; }

Graph in GraphViz

5
Other visualization tools
D3.js
D3 allows you to bind arbitrary data to a Document Object Model (DOM), and then apply
data-driven transformations to the document. For example, you can use D3 to generate an
HTML table from an array of numbers. Or, use the same data to create an interactive SVG
bar chart with smooth transitions and interaction. D3 is not a monolithic framework that
seeks to provide every conceivable feature. Instead, D3 solves the crux of the problem:
efficient manipulation of documents based on data. This avoids proprietary representation
and affords extraordinary flexibility, exposing the full capabilities of web standards such as
HTML, SVG, and CSS. With minimal overhead, D3 is extremely fast, supporting large
datasets and dynamic behaviors for interaction and animation. D3’s functional style allows
code reuse through a diverse collection of official and community-developed modules.

A graph in D3.js

Cytoscape
Cytoscape is an open-source software platform for visualizing molecular interaction
networks and biological pathways and integrating these networks with annotations, gene
expression profiles, and other state data. Although Cytoscape was originally designed for
biological research, now it is a general platform for complex network analysis and
visualization. Cytoscape core distribution provides a basic set of features for data
integration, analysis, and visualization. Additional features are available as Apps (formerly
called Plugins). Apps are available for network and molecular profiling analyses, new
layouts, additional file format support, scripting, and connection with databases. They
may be developed by anyone using the Cytoscape open API based on Java™ technology
and App community development is encouraged. Most of the Apps are freely available
from Cytoscape App Store.

6
A graph in Cytoscape

Gephi
Gephi is open-source software for visualizing and analyzing large network graphs. Gephi
uses a 3D render engine to display graphs in real-time and speed up the exploration. You
can use it to explore, analyze, spatialize, filter, manipulate and export all types of graphs.

A graph in Gephi

You might also like