Drawing tool Graphviz learning and use

Source: Internet
Author: User
Tags in degrees

Drawing tool Graphviz learning and use
Cause

I recently completed my graduation project. When I was writing my graduation thesis, the teacher recommended the use of code to draw related figures. At that time, the time was a little tight, so I didn't do this. I recently read the rapidjson document and see that all the images in the miloyip document usedotThe source code of the file is compiled. Check the source code of graphviz. It took me a long time to learn about it.graphvizSyntax and use, I feel very good, there will be a good tool for drawing later.

graphvizIt is a widely used command line drawing software, but it is a drawing software. It can draw a picture that is not a cartoon or logo that most people think, but a mathematicalgraphThe common saying is "Relationship Diagram 」.

Install

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

  • Ubuntu or Debian is easy to install:

      sudo apt-get install graphviz
  • Use brew in Mac

      brew install graphviz
  • Windows reference http://www.graphviz.org/Download_windows.php

Getting started

1. WritedotSource code file with a suffix, such:

// Hello. dot digraph {

    hello -> world;}

2. UsedotCommand compilation, such

Dot hello. dot-T png-o hello.png

The complete command is:

<Cmd> <inputfile>-T <format>-o <outputfile>

The graphviz<cmd>There are several different types of images, each of which is used in the same way. The difference is that the rendered images have different effects.manThe introduction in is as follows:

<Cmd> Introduction
Dot The rendered graph has clear directionality.
Neato The rendered graph lacks directionality.
Twopi The rendered graph uses a radioactive layout.
Circo The rendered graph adopts a ring layout.
Fdp The rendered graph lacks directionality.
Sfdp The rendering of large images lacks directionality.

You canman <cmd>For further instructions. However, it is easier to use it in person. In this article, any graph that is not described isdotRendered.

3. view results

The effect is as follows:

Syntax Introduction

Graphviz uses the dot syntax. Other syntaxes can be formally described as follows:

graph         = [strict] (digraph | graph) id ’{’ stmt-list ’}’stmt-list     = [stmt [’;’] [stmt-list ] ]stmt          = attr-stmt | node-stmt | edge-stmt | subgraph | id ’=’ id attr-stmt     = (graph | node | edge) attr-listattr-list     = ’[’ [a-list ] ’]’ [attr-list] a-list        = id ’=’ id [’,’] [a-list] node-stmt     = node-id [attr-list]node-id       = id [port]port          = port-location [port-angle] | port-angle [port-location] port-location = ’:’ id|’:’ ’(’id’,’id’)’port-angle    = ’@’ idedge-stmt     = (node-id | subgraph) edgeRHS [attr-list]edgeRHS       = edgeop (node-id | subgraph) [edgeRHS]subgraph      = [subgraph id] ’{’ stmt-list ’}’ | subgraph id

Graphviz has two types of graphs. One is an undirected graph.graph, Edge use--Connection, a Directed Graphdigraph, Edge use->Connection. This can be a simple practice.

The specific graph description includes the attributes (attr), node, edge, and subgraph descriptions.

The node attributes are as follows:

Name Default Values
Color Black Node shape color
Comment   Any string (format-dependent)
Distortion 0.0 Node distortion for shape = polygon
Fillcolor Lightgrey/black Node fill color
Fixedsize False Label text has no affect on node size
Fontcolor Black Type face color
Fontname Times-Roman Font family
Fontsize 14 Point size of label
Group   Name of node's group
Height . 5 Height in inches
Label Node name Any string
Layer Overlay range All, id or id: id
Orientation 0.0 Node rotation angle
Peripheries Shape-dependent Number of node boundaries
Regular False Force polygon to be regular
Shape Ellipse Node shape; see Section 2.1 and Appendix E
Shapefile   External EPSF or SVG custom shape file
Sides 4 Number of sides for shape = polygon
Skew 0.0 Skewing of node for shape = polygon
Style   Graphics options, e.g. bold, dotted, filled; cf. Section 2.3
URL   URL associated with node (format-dependent)
Width . 75 Width in inches
Z 0.0 Z coordinate for fig output

Edge attributes are as follows:

Name Default Values
Arrowhead Normal Style of arrowhead at head end
Arrowsize 1.0 Scaling factor for arrowheads
Arrowtail Normal Style of arrowhead at tail end
Color Black Edge stroke color
Comment   Any string (format-dependent)
Constraint True Use edge to affect node ranking
Decorate   If set, draws a line connecting labels with their edges
Dir Forward Forward, back, both, or none
Fontcolor Black Type face color
Fontname Times-Roman Font family
Fontsize 14 Point size of label
Headlabel   Label placed near head of edge
Headport   N, ne, e, se, s, sw, w, nw
HeadURL   URL attached to head label if output format is ismap
Label   Edge label
Labelangle -25.0 Angle in degrees which head or tail label is rotated off edge
Labeldistance 1.0 Scaling factor for distance of head or tail label from node
Labelfloat False Lessen constraints on edge label placement
Labelfontcolor Black Type face color for head and tail labels
Labelfontname Times-Roman Font family for head and tail labels
Labelfontsize 14 Point size for head and tail labels
Layer Overlay range All, id or id: id
Lhead   Name of cluster to use as head of edge
Ltail   Name of cluster to use as tail of edge
Minlen 1 Minimum rank distance between head and tail
Samehead   Tag for head node; edge heads with the same tag are
Sametail   Merged onto the same port
Style   Tag for tail node; edge tails with the same tag are merged onto the same port
Taillabel   Graphics options, e.g. bold, dotted, filled; cf. Section 2.3
Tailport   Label placed near tail of edge n, ne, e, se, s, sw, w, nw
TailURL   URL attached to tail label if output format is ismap
Weight 1 Integer cost of stretching an edge

The figure attributes are as follows:

Name Default Values
Bgcolor   Background color for drawing, plus initial fill color
Center False Center drawing on page
Clusterrank Local May be global or none
Color Black For clusters, outline color, and fill color if fillcolor not defined
Comment   Any string (format-dependent)
Compound False Allow edges between clusters
Concentrate False Enables edge concentrators
Fillcolor Black Cluster fill color
Fontcolor Black Type face color
Fontname Times-Roman Font family
Fontpath   List of directories to search for fonts
Fontsize 14 Point size of label
Label   Any string
Labeljust Centered "L" and "r" for left-and right-justified cluster labels, respectively
Labelloc Top "T" and "B" for top-and bottom-justified cluster labels, respectively
Layers   Id: id...
Margin . 5 Margin included in page, inches
Mclimit 1.0 Scale factor for mincross iterations
Nodesep . 25 Separation between nodes, in inches.
Nslimit   If set to f, bounds network simplex iterations by (f) (number of nodes) when setting x-coordinates
Nslimit1   If set to f, bounds network simplex iterations by (f) (number of nodes) when ranking nodes
Ordering   If out edge order is preserved
Orientation Portrait If rotate is not used and the value is landscape, use landscape orientation
Page   Unit of pagination, e.g. "8.5, 11"
Pagedir BL Traversal order of pages
Quantum   If quantum limit 0.0, node label dimensions will be rounded to integral multiples of quantum
Rank   Same, min, max, source or sink
Rankdir TB LR (left to right) or TB (top to bottom)
Ranksep . 75 Separation between ranks, in inches.
Ratio   Approximate aspect ratio desired, fill or auto
Remincross   If true and there are multiple clusters, re-run crossing minimization
Rotate   If 90, set orientation to landscape
Samplepoints 8 Number of points used to represent ellipses and circles on output (cf. Appendix C
Searchsize 30 Maximum edges with negative cut values to check when looking for a minimum one during network simplex
Size   Maximum drawing size, in inches
Style   Graphics options, e.g. filled for clusters
URL   URL associated with graph (format-dependent)
Instance

The above syntax is introduced. In actual use, we will learn several detailed examples. Below is a binary tree code:

digraph g {    node [shape = record,height=.1];    node0[label = "<f0> |<f1> G|<f2> "];    node1[label = "<f0> |<f1> E|<f2> "];    node2[label = "<f0> |<f1> B|<f2> "];    node3[label = "<f0> |<f1> F|<f2> "];    node4[label = "<f0> |<f1> R|<f2> "];    node5[label = "<f0> |<f1> H|<f2> "];    node6[label = "<f0> |<f1> Y|<f2> "];    node7[label = "<f0> |<f1> A|<f2> "];    node8[label = "<f0> |<f1> C|<f2> "];    "node0":f2 -> "node4":f1;    "node0":f0 -> "node1":f1;    "node1":f0 -> "node2":f1;    "node1":f2 -> "node3":f1;    "node2":f2 -> "node8":f1;    "node2":f0 -> "node7":f1;    "node4":f2 -> "node6":f1;    "node4":f0 -> "node5":f1;}

The effect is as follows:

Python call

In additiondotIn addition to writing graphics, you can also use python to write relevant code to generate graphical files,

Installation Method:

pip install pygraphviz

ReferencePyGraphviz:

import pygraphviz as pgv

Initialize graph class:

G=pgv.AGraph()

Add nodes and edges:

G.add_node('a') # adds node 'a'G.add_edge('b','c') # adds edge 'b'-'c' (and also nodes 'b', 'c')

Set attributes:

G.graph_attr['label']='test graphf'G.node_attr['shape']='circle'G.edge_attr['color']='red'

Set the output format:

G.layout() # default to neatoG.layout(prog='dot') # use do

Output to file:

G.draw('file.png')  # write previously positioned graph to PNG fileG.draw('file.ps',prog='circo') # use circo to position, write PS file

Effect:

Reference
  • Dotguide
  • Solve Graphviz usage and Chinese garbled characters
  • Rapidjson
  • PyGraphviz tutorial

This article permanently updates the link address:

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.