Visualizing a graph network
Graphical networks of edges and nodes can be difficult to debug or comprehend, and thus, visualization helps tremendously. In this recipe, we will convert a graph data structure into an image of nodes and edges.
Getting ready
To use Graphviz, the graph visualization library, we must first install it on the machine. The official website of Graphviz contains the download and installation instructions (http://www.graphviz.org). On Debian-based operating systems, Graphviz can be installed using apt-get
as follows:
$ sudo apt-get install graphviz-dev graphviz
Next, we need to install the Graphviz Haskell bindings from Cabal as follows:
$ cabal install graphviz
How to do it…
- Import the relevant libraries as follows:
import Data.Text.Lazy (Text, empty, unpack) import Data.Graph.Inductive (Gr, mkGraph) import Data.GraphViz (GraphvizParams, nonClusteredParams, graphToDot) import Data.GraphViz.Printing (toDot, renderDot)
- Create a graph defined by identifying the pairs...