Wednesday, November 2, 2011

Graph's visualization in Linux

A few days ago my friend show me a very simple and convenient program to visualize graphs - "graphviz". It's free and can be used in Linux, Solaris, Windows and Mac. For drawing graphs "graphviz" uses DOT language.

Let's create a simple graph. Open text editor and paste this code:

digraph SimpleGraph {
    node [shape=circle];
    node [style=dashed];
    0 -> 1 [label="A"];
    node [style=solid];
    0 -> 2 [label="B"];
    1 -> 3 [label="C"];
    2 -> 1 [label="D", style=dotted];
    2 -> 3 [label="E", style=dotted];
    2 -> 4 [label="F"];
    3 -> 5 [label="G"];
    4 -> 5 [label="H"];

  • digraph - this graph keyword allows to build a directed graph;
  • rankdir=LR - allows to set graph orietation (from left to right), by default graph orientation is from top to bottom;
  • [shape=circle] - setting node form;
  • ranksep=0.5 - distance between nodes;
  • node [style=dashed] - setting line form for node.

Save file with .gv extension. Open terminal and enter this command which allows to import our graph to .png format:

$ dot -T png simple_graph.gv > simple_graph.png


$ dot -T png simple_graph.gv -o simple_graph.png

Let's see our graph in graphical representation:

Istead .png you may use one of these formats:
bmp canon cmap cmapx cmapx_np dot eps fig gd gd2 gif gtk gv ico imap imap_np ismap jpe jpeg jpg pdf plain plain-ext png ps ps2 svg svgz tga tif tiff tk vml vmlz vrml wbmp x11 xdot xlib

Here you can download "graphviz" for your system -
Here you can find a good HOWTOs -

Node, Edge and Graph Attributes

No comments:

Post a Comment