1. graphviz docker

1
2
3
4
5
6
7
8
9
10
FROM ubuntu:18.04

ENV LANG C.UTF-8
WORKDIR /code
COPY . .

RUN sed -i s@/deb.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list && \
sed -i s@/security.debian.org/@/mirrors.aliyun.com/@g /etc/apt/sources.list && \
apt-get clean && apt-get update && apt-get install -y python3-pip python3-dev build-essential git graphviz fonts-arphic-ukai

2. networkx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from pprint import pprint

import networkx as nx
import matplotlib.pyplot as plt


def list_fonts():
from matplotlib.font_manager import FontManager
fm = FontManager()
pprint(set([f.name for f in fm.ttflist]))


def draw():
"""
列出系统的字体,然后找一个支持中文的
"""
plt.rcParams['font.sans-serif'] = ['AR PL UKai CN']
# 创建一个NetworkX图
G = nx.Graph()
G.add_edges_from([('你呀你呀', 2), (2, 3), (3, 1)])

# 绘制图形
pos = nx.spring_layout(G) # 选择布局算法
nx.draw(G, pos, with_labels=True) # , node_color='lightblue', edge_color='gray') # 绘制图形
plt.savefig('graph.png') # 保存为PNG图像文件
plt.show() # 显示图形(可选)


if __name__ == '__main__':
# list_fonts()
draw()