Graphviz-use commands to draw a graph!

Source: Internet
Author: User

Graphviz-use commands to draw a graph!
Introduction

Graphviz is a widely used command line drawing software, but it is a drawing software. It is not a cartoon or logo that most people think, but a graph in the mathematical sense ", the common saying is "Relationship Diagram 」.

For example, see the following figure:

▲Figure 1: Unix family. One of the demonstration images on the Graphviz official website.

It is painful to use a hand-painted image, and Graphviz can fix it for the user. Graphviz provides a set of languages that allow you to directly state the node, edge, and direction of an image. Then, it will generate the entire image for you.

There are many types of images Graphviz can draw. You can find more examples on the official website.

Install

Graphviz supports Windows, Mac OS X, FreeBSD, Solaris, Linux, and other operating systems.

If you are a Linux User and are famous for using this software, you will probably find it in the Suite Manager. Install it from the suite library! If you cannot find the source code, go to the download page on the official website and try the source code.

If you are a Windows user, please go here to download the Installation File: http://www.graphviz.org/Download_windows.php

Mac OS X users please go here: http://www.graphviz.org/Download_macos.php

Use of Graphviz
1 # Graphviz2> <cmd> <inputfile>-T <format>-o <outputfile> 3 4 # example: Output png5> dot input. dot-T png-o output.png 6 # example: the output is png, but the file name is txt7> dot input. dot-T png-o output.txt

First, let's take a look at the <cmd> section above.

There are several <cmd> Graphviz methods, each of which is used in the same way. The difference is that the rendered images have different effects. The introduction in man is written in this way ......

Dot

The rendered graph has clear directionality.

Neato

The rendered graph lacks directionality.

Twopi

The rendered image is replaced by a radioactive environment.

Circo

The rendered graph is replaced by a ring chart.

Fdp

The rendered graph lacks directionality.

Sfdp

The rendering of large images lacks directionality.

For more information, see man <cmd>. However, it is easier to use it in person. In this article, any graph that is not illustrated is rendered by dot by default.

Continue. In Graphviz, if you do not specify the-T parameter, Graphviz will not automatically guess what format you want to generate, but will only render it in the preset format. There are many optional formats, including (but not limited to) jpg, png, svg, and so on. The list of all formats can be found at the bottom of the official website description page.

-O allows you to specify the file name for storing files. If you do not use the-o option to specify the output file name, Graphviz outputs the result to the standard output.

Unless used in a special way, you must remember to issue these two parameters each time.

Dot language description

The language used for drawing Graphviz is "dot ". The following describes how to use it.

Directed Graph and undirected graph

When using the dot language, the first step is to decide which image to draw.

Graphs are divided into two types: Directed Graphs and undirected graphs.

▲Figure 2: Directed Graph

▲Figure 3: undirected graph

A digraph is used to declare an image. The relationship between nodes is written as "-> ".

1/* demo1. By the way, you can use the allow solution in C ++ in the dot language. In this action, the C-style ultimate solution */2 digraph demo1 {// is also the ultimate solution, and the C ++ style. 3 a-> B-> c; 4 c-> a; 5}

An undirected graph is used to declare an image. The relationship between nodes can be written "--".

1  //demo22  graph demo2{3       a -- b -- c;4       c -- a;5  }

Demo1 and demo2 are image names, which are usually left blank.

Use quotation marks

In addition to being used as a recognition word in the program, a, B, and c in the preceding Section also become the display name (label) of the node ). However, if the name contains Chinese characters or spaces, Graphviz may make a mistake.

To prevent unnecessary misunderstandings, it is recommended that you enclose them in quotation marks. As shown below:

1 // demo32 digraph {3 "total attack"-> "attacked"; 4 "strong attack"-> "attacked"; 5 "Healthy attack"-> "attacked "; 6}

▲Figure 4: A blank demonstration is mixed. Render uses fdp for rendering.

So there is no problem!

Subgraph and simplification Techniques

Let's look at a complicated example. This is a diagram of the major link of the Mediterranean Sea:

1 graph G {2 "" -- "sub-sea "; 3 "Black Sea" -- "bospos strait" 4 "dardanier strait" -- "Aegean Sea" 5 subgraph cluster_T {// Xindong West 6 label = "Heihe strait "; // Xindong West 7 "dardanier strait" -- "marmarathon" -- "bospuls strait"; 8} 9 subgraph cluster_M {10 label = "Mediterranean Sea "; 11 "central Mediterranean" -- {"Aegean Sea" "Ionia" sisily strait "}; // It is also new things 12 "Western Mediterranean" -- {"sisisily strait" "thineean sea" "ligoria sea" "ibri sea" "alwallan sea "}; 13 "The Ionia sea" -- "The ardonia sea"; 14 "The arworan sea" -- "the Strait of Gibraltar"; 15} 16}

▲Figure 5: Connecting map of the Mediterranean Sea, rendered using fdp, with 8 seeds (-Gstart = 8 ).

This figure shows some new things.

The first is the subgraph keyword. As shown in the name, it is used to define the "secondary image.

The secondary image is often called the cluster subgraph in the dot official file. It refers to the two pieces wrapped in boxes in the graph. Its definition method is very similar to that of the general graph, however, there are two things to pay attention:

  1. The name starts with the cluster prefix. Otherwise, although the syntax can pass, it does not produce the expected effect on the image.
  2. If the parent graph is an undirected graph, it must also be an undirected graph. If the parent graph is a directed graph, you must follow it here.

The second focus is the following section:

1 "central Mediterranean" -- {"Aegean Sea" "Ionia" sisily strait "};

Start with braces and separate them with spaces-this is a way to group several nodes in one breath and operate simultaneously, which is equivalent:

1 "central Mediterranean" -- "Aegean Sea"; 2 "central Mediterranean" -- "Ionia sea"; 3 "central Mediterranean" -- "sisily strait ";

You can even use the following code to plot Figure 6:

1  digraph G{2     {a b c} -> {d e f}3  }

▲Figure 6: Effect of braces

This syntactic sugar is very convenient and can be used flexibly.

The third difference lies in the line label = XXX. This is the "attribute" method.

For more information about attributes, see the following chapter.

Attribute

With the techniques described above, all the drawing relationships can be drawn smoothly.

However, we usually do some special processing on images when drawing images. For example, bold words, color-changing graphs, changing the shape of labels or connections, and horizontal alignment of some nodes.

To control these things, you need to use attributes.

There are four types of attributes:

  1. Used on nodes (Node, N)
  2. Used on line segments (Edge, E)
  3. Used on the Root image (Graph, G)
  4. Used on the sub-image (Cluster subgraph, C)

You can read the tables in the manual to determine which attributes can be used.

So how can attributes be used?

Apply attributes

If you want to set the attributes of the root image or sub-image, you must specify the attributes in the brackets of the image as shown in the preceding example ......

Attribute name = value;

That's all.

The attributes of a node can be specified as follows:

1 node name [node attribute name = value]; 2 node name [node attribute name = value, node attribute name = value]; 3 node [node attribute name = value, node attribute name = value];

The statement specified by the attribute must be enclosed in brackets. When multiple values are specified at a time, they must be separated by commas.

The node in the third row is a keyword used to represent all "not created" nodes in the "image range", or you can understand it as: within the range of the current braces, this statement changes the default attribute values of all nodes that have not yet been created.

Specify the attributes of a line segment (edge), similar to that of a node:

1 node name-> node name [Line Segment attribute name = value]; 2 node name -- node name [Line Segment attribute name = value, line segment attribute name = value]; 3 edge [Line Segment attribute name = value, line segment attribute name = value];

Edge is the keyword.

Here, I would like to add a line segment concept: some line segment-related attributes have head and tail values. The head and tail mentioned here must be like an arrow (like a-> B ).

For a line segment, the head of this arrow is the head. So, this may be different from what you initially imagined, because the "Head" mentioned here is actually the one behind the two nodes.

Attribute example

Add some attributes to the previous examples.

1 graph G {2 "" [shape = circle, color = blueviolet, fontcolor = bluevilet, fontsize = 20]; 3 "" -- "" [label = ""]; 4 5 subgraph cluster_T {6 label = ""; 7 fontsize = 24; 8 fillcolor = darkslategray; 9 style = filled; 10 fontcolor = white; 11 node [fontcolor = white, color = white]; 12 "bosps strait" -- "Marmara Sea" -- "dardanier strait" [color = white]; 13 "bospos strait" [shape = parallelogram]; 14 "dardanier strait" [shape = parallelogram]; 15} 16 17 "" -- "bossspus strait" [color = red, penwidth = 2]; 18 "dardanier strait" -- "Aegean Sea" [color = red, penwidth = 2]; 19 20 subgraph cluster_M {21 label = "Mediterranean Sea"; 22 fontsize = 24; 23 "Western Mediterranean" [shape = Mcircle, style = filled, color = gray, fillcolor = aquamarine, fontsize = 20]; 24 "central Mediterranean" [shape = Mcircle, style = filled, color = gray, fillcolor = aquamarine, fontsize = 20]; 25 "Strait of Gibraltar" [shape = parallelogram, fontcolor = red]; 26 "sisilisi strait" [shape = parallelogram]; 27 "central Mediterranean" -- {"Aegean Sea" "Ionia" sisisi strait "}; 28 "Western Mediterranean" -- {"sisisily strait" "thineean sea" "ligoria sea" "ibbia sea" "alworan sea"}; 29 "Ionia sea" -- "adariya sea "; 30 "alworanhai" -- "Strait of Gibraltar"; 31} 32}

▲Figure 7: Connecting map of the Mediterranean Sea (adding attributes ). Use fdp rendering. The seed is 3 (-Gstart = 3 ).

Among the many attributes, label is the most commonly used.

Label determines what to display on a node, line segment, or sub-image. If your node name is long, you can get a brief name inside the program, and then operate on it through the short name, and specify its display content through the label.

Color, fillcolor, and fontcolor attributes are used to control colors. However, fillcolor takes effect only when the style is specified as "filled.

Shape: You can specify the shape of a node. For more information about the shape list, see.

Line Segment attributes. The arrows in the directed graph can be used to specify the head and tail styles through the arrowhead and arrowtail attributes. As for the line segment itself, you can specify different types of dotted lines and short cut lines through the style nature. You can also use the dir attribute to reverse the arrow direction.

In addition, there is an image attribute that allows the node to display images. You can also refer to it when necessary.

There are too many attributes to be described one by one. Please refer to the Manual on the official website for comprehensive information.

Rank

The dot language has a concept called rank.

In the dot language, rank is similar to "level 」. It is mainly used in the dot Renderer.

See the following figure:

▲Figure 8: rank example.

Obviously, the picture is divided into four layers from top to bottom-this is rank.

Below is the dot statement ......

1  digraph demo{2     a -> b -> c -> d;3     b -> {e f};4  }

Observe the code to see how rank is specified.

The basic rule is that the header of each line segment has an extra level than the tail end (the bottom layer above the graph ).

But wait, what if the statements specified by the level conflict with each other?

Modify the code above ......

1 digraph demo {2 a-> B-> c-> d [label = "rank increase"]; 3 B-> {e f} [label = "rank increase"]; 4 f-> a [label = "do not affect rank"]; 5}

▲Figure 9: rank example.

Based on the above results, it is clear that rank is set to "first win first.

In addition to the basic rules, rank can also be adjusted through attributes. If necessary, capture the manual and check it.

References
  1. The attribute manual on the official website. Check the attributes here:
    Http://www.graphviz.org/content/attrs.
  2. Official Website Graphviz instructions and dot language manual:
    Http://www.graphviz.org/doc/dot.ref.
  3. If you prefer the pdf version, please download it here:
    Http://www.graphviz.org/doc/dotguide.htm.

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.