python画拓扑图子图_使用 Graphviz 画拓扑图

使用 Graphviz 画拓扑图

0)前述

本文着重讲一下 Graphviz 的风格控制,基础一些的就不在这里讲啦。

1)从 Record 开始

下面通过一个简单示例开始吧:

在 Graphviz 的文档页有如下一张图,下面就用它里开始练习了。

简单的 Record 风格

这幅图的dot代码如下:

01digraph A {02 node [shape=record];03 struct1 [label=" left| middle| right"];04 struct2 [label=" one| two"];05 struct3 [label="hello \n world |{ b |{c| d|e}| f}| g | h"];06 struct1:f1 ->struct2:f0;07 struct1:f2 ->struct3:here;08 }

第一行,声明图的类型为 "digraph" 和图的ID 为 "A"

第二行,声明本图的node的类型均为Record

第三行,声明了一个结构体,并且结构体分为三部分,分别为f0,f1,f2。f0的名称为left,f1的名称为middle,f2的名称为right。这三部分用"|"隔开,并且他们都属于这个struct1这个结构体的label.

第四行,声明了struct2。

第五行,看起来稍微复杂些。这一行声明了结构体struct3,并且使用了三层嵌套。第一层是“helloworld”、“b,c,d,e,f”、“g”和“h”。第二层是“b,c,d,e,f”这一组被分割成了三部分:“b”、"c,d,e"和"f"。第三层是"c,d,e"又被分割成了"c"、“d“和“e”三部分。嵌套的时候,使用大括号进行隔离。

第六~七行,开始建立node间的关系。struct1的f0指向struct2的f0;struct1 的 f2 指向 struct3 的 here。

从这段脚本代码的直观感受,dot脚本语言是一个描述型语言。 相信参照这个例子,已经可以写出关系清晰的拓扑图了。

接下来我们再来看一下另一种写法。

digraph structs {

node [shape=plaintext]

struct1 [label=<

left

middle

right

>];

struct2 [label=<

one

two

>];

struct3 [label=<

hello
world

b

g

h

c

d

e

f

>];

struct1:f1 -> struct2:f0;

struct1:f2 -> struct3:here;

}

这段代码就不详细解释了,对照着图像仔细想一想,应该也能想明白。熟悉HTML同学应该一眼就看明白了,其中的table、tr、td等,就是HTML里面的table。照着这个思路去看,应该能理解的更快。其中的COLSPAN等关键字在文档页中可以找到其具体含义。

我想分析一下下面这个图:

代码如下(或者这里)

01 digraph G {

02 rankdir=LR

03 node [shape=plaintext]

04

05 a [

06 label=<07

>

15 ]

16

17 b [shape=ellipse style=filled

18 label=<19

corn
f
3839penguin40414244344>

45 ]

46

47 c [

48 label=<49long line 1
line 2line 3>

50 ]

51

52 subgraph { rank=same b c }

53 a:here -> b:there [dir=both arrowtail = diamond]

54 c -> b

55 d [shape=triangle]

56 d -> c [label=<57

Edge labels
also
>

64 ]

65 }

分析

第5行到第15行,定义了一个 node,这个 node 的label是一个table。图片的左下角就是这个node啦。

第7行,定义了这个table的属性,属性值如下:

属性具体含义

BORDER="value"

specifies the width of the border around the object in points

CELLBORDER="value"

specifies the width of the border for all cells in a table. It can be overridden by a BORDER tag in a cell.

CELLSPACING="value"

specifies the space, in points, between cells in a table and between a cell and the table's border.

ROWSPAN="value"

specifies the number of rows spanned by the cell.

BGCOLOR="color"

sets the color of the background. This color can be overridden by a BGCOLOR attribute in descendents.

PORT="value"

attaches a portname to the object. (See portPos.)

COLSPAN="value"

specifies the number of columns spanned by the cell.

第8~13行,定义了两个单元格。

第17~45行 定义了node :b。

第17行,指明了node的shape是ellipse(椭圆),style是filled。

第19~40行,这段代码直接扔到html页面里面,也可以看到差不多相同的效果:

elephant

two

corn

c

f

penguin

4

感觉不需要再写下去了,再写下去的话就是在写html了。所以放一个html的table教程链接吧。http://www.w3school.com.cn/tags/tag_table.asp

所以学好html是关键!!!

第53~56行,建立这几个node之间的关系。

Graphviz 官网的几个文档

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python有很多绘制网络拓扑图的库,其中比较常用的有NetworkX、Graphviz、pydot等。下面以NetworkX为例介绍如何绘制网络拓扑图。 1. 安装NetworkX 在终端或命令行中输入以下指令安装NetworkX: ``` pip install networkx ``` 2. 创建网络拓扑图Python中,我们可以使用NetworkX库创建一个空的有向图,然后添加节点和边来构建网络拓扑图。以下是一个简单的例子: ```python import networkx as nx import matplotlib.pyplot as plt # 创建空的有向图 G = nx.DiGraph() # 添加节点 G.add_node("A") G.add_node("B") G.add_node("C") G.add_node("D") # 添加边 G.add_edge("A", "B") G.add_edge("A", "C") G.add_edge("B", "D") G.add_edge("C", "D") # 绘制网络拓扑图 nx.draw(G, with_labels=True) plt.show() ``` 运行以上代码,就可以得到一个简单的网络拓扑图。 3. 绘制更复杂的网络拓扑图 除了添加节点和边之外,我们还可以通过设置节点和边的属性来绘制更复杂的网络拓扑图。以下是一个更复杂的例子: ```python import networkx as nx import matplotlib.pyplot as plt # 创建空的有向图 G = nx.DiGraph() # 添加节点 G.add_node("A", pos=(0, 1)) G.add_node("B", pos=(1, 2)) G.add_node("C", pos=(1, 0)) G.add_node("D", pos=(2, 1)) G.add_node("E", pos=(3, 2)) G.add_node("F", pos=(3, 0)) G.add_node("G", pos=(4, 1)) # 添加边 G.add_edge("A", "B", weight=3) G.add_edge("A", "C", weight=2) G.add_edge("B", "D", weight=4) G.add_edge("C", "D", weight=1) G.add_edge("D", "E", weight=2) G.add_edge("D", "F", weight=3) G.add_edge("E", "G", weight=1) G.add_edge("F", "G", weight=2) # 获取节点的位置信息 pos = nx.get_node_attributes(G, 'pos') # 获取边的权重信息 edge_labels = nx.get_edge_attributes(G, 'weight') # 绘制网络拓扑图 nx.draw_networkx_nodes(G, pos, node_size=500) nx.draw_networkx_labels(G, pos) nx.draw_networkx_edges(G, pos, arrows=True) nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_color='red') plt.axis("off") plt.show() ``` 运行以上代码,就可以得到一个更复杂的网络拓扑图。在这个例子中,我们设置了节点的位置信息和边的权重信息,然后使用绘图函数绘制了节点和边,并在边上标注了权重。需要注意的是,这个例子中我们使用了`draw_networkx_`系列函数来绘图,因为它们比`draw`函数更加灵活和强大。 以上就是使用NetworkX绘制网络拓扑图的简单介绍。如果想了解更多关于NetworkX的用法和绘图函数的参数,请参考官方文档:https://networkx.github.io/documentation/stable/index.html。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值