networkx 思维导图 python

阅读 52

2023-10-26

import networkx as nx
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

# 设置字体
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14)  # 这里使用了宋体,你可以根据需要更改字体路径和名称

# 创建一个空的有向图
G = nx.DiGraph()

# 添加节点和边
G.add_edge('Python', '语法')
G.add_edge('Python', '库')
G.add_edge('语法', '变量')
G.add_edge('语法', '循环')
G.add_edge('语法', '条件')
G.add_edge('库', 'NumPy')
G.add_edge('库', 'Pandas')
G.add_edge('库', 'Matplotlib')

# 绘制图形
pos = nx.spring_layout(G)
ax = plt.gca()
nx.draw(G, pos, node_color='skyblue', edge_color='gray', node_size=1500, alpha=0.7, ax=ax)

# 绘制标签
for node, (x, y) in pos.items():
    plt.text(x, y, node, fontsize=14, ha='center', va='center', fontproperties=font)

# 显示图形
plt.show()

精彩评论(0)

0 0 举报