Preparatory knowledge

Install Graphviz

Step 1: Install Graphviz on the official Graphviz website (downloadable.msi and installation-free versions), and then configure the bin directory into the environment variable Path

Step 2: Install Graphviz from the command line. This module provides a number of Python interfaces to use graphviz software

pip install graphviz

What is python-graphviz?

Official document

Graphviz is an open source graphic visualization software developed by AT&T Labs Research. Graphic visualization is a way to represent structural information as abstract graphics and network diagrams. It has important applications in web, bioinformatics, software engineering, database and web design, machine learning, and visual interfaces in other technical fields.

The Graphviz layout program takes descriptions of graphics in a simple text language and makes diagrams in useful formats, such as images and SVG for web pages. PDF or Postscript for inclusion in other documents; Or display in an interactive graphics browser. Graphviz has many useful features for specific charts, such as colors, fonts, table node layout, line styles, hyperlinks, and options for custom shapes.

Graphviz is a graphing tool that draws tree graphs from dot scripts, == We can use Python code to generate the dot script ==, and then call Graphviz to parse the script and generate an image.

Dot scripting language is a scripting language for drawing on the open source toolkit Graphviz. The syntax is very simple and the official documentation is only 8 pages long.

case

Case 1:

from graphviz import Digraph
dot = Digraph(comment='The Test Table')Annotate the DOT scripting language

#---------- create node statement ----
# add Dot A,A is labeled Dot A
dot.node('A'.'Dot A')
Add Dot B with the tag Dot B
dot.node('B'.'Dot B')
# add Dot C, which is labeled Dot C
dot.node(name='C', label= 'Dot C',color='red')

#---------- create edge statement ----- (two ways)
Create a stack of edges, that is, connect two edges of AB and one edge of AC.
dot.edges(['AB'.'AC'])
# Create an edge between the two dots
dot.edge('B'.'C'.'test')

#-------- Watch the original dot language expression -----
Get the DOT source as a string, as shown in Figure 1
print(dot.source)

#-------- display image -----------
#dot.view() is displayed directly, using the digraph.gv default name
dot.render('test-table.gv', view=True)# Set the image name and display it directly
Copy the code

Dot script primitives:

Graphic display:

Case 2:

from graphviz import Digraph


sub_g0 = Digraph(comment="process1",graph_attr={"style":'filled'."color":'lightgrey'},node_attr={"style":"filled"."color":"red"})
sub_g0.node("a0"."a0")
sub_g0.node("a1"."a1")
sub_g0.node("a2"."a2")
sub_g0.node("a3"."a3")
sub_g0.edge("a0"."a1")
sub_g0.edge("a1"."a2")
sub_g0.edge("a2"."a3")
sub_g0.edge("a3"."a0")

sub_g1 = Digraph(comment="process1",graph_attr={"style":'filled'})
sub_g1.node("B"."b0")
sub_g1.node("C"."b1")
sub_g1.node("D"."b2")
sub_g1.node("E"."b3")
sub_g1.edges(["BC"."CD"."DE"])

grap_g = Digraph("G".format="pdf")

grap_g.node(
"start", label="start",shape="Mdiamond")
grap_g.node(
"end", label="end", shape="Mdiamond")

grap_g.subgraph(sub_g0)  Add the Digraph for sub_g0 and sub_g0 to the Digraph for grap_g
grap_g.subgraph(sub_g0)

grap_g.edge("start"."a0")
grap_g.edge("start"."B")

grap_g.edge("a1"."E")
grap_g.edge("D"."a3")

grap_g.edge("a3"."end")
grap_g.edge("E"."end")

grap_g.view()
#grap_g.render('test-table2.gv', view=True)
Copy the code