在 Python 中生成思维导图图片有多种方法,以下是几种常用的实现方式:
方法一:使用 graphviz 库from graphviz import Digraph
def create_mind_map():
# 创建有向图
dot = Digraph(comment='思维导图', format='png')
# 添加中心节点
dot.node('A', '中心主题')
# 添加一级分支
dot.node('B', '分支主题1')
dot.node('C', '分支主题2')
dot.node('D', '分支主题3')
# 添加二级分支
dot.node('B1', '子主题1.1')
dot.node('B2', '子主题1.2')
dot.node('C1', '子主题2.1')
# 连接节点
dot.edges(['AB', 'AC', 'AD'])
dot.edges(['BB1', 'BB2', 'CC1'])
# 设置图形属性
dot.attr('node', shape='box', style='rounded', color='blue')
dot.attr('edge', arrowhead='none')
# 生成并保存图片
dot.render('mind_map', view=True)
create_mind_map() 方法二:使用 pygraphviz 库 import pygraphviz as pgv
def create_mind_map():
# 创建图形
G = pgv.AGraph(directed=True, strict=False)
# 添加节点和边
G.add_node("中心主题", shape="ellipse", style="filled", color="lightblue")
# 一级分支
G.add_node("分支1", shape="box")
G.add_node("分支2", shape="box")
G.add_node("分支3", shape="box")
# 二级分支
G.add_node("子分支1.1", shape="note")
G.add_node("子分支1.2", shape="note")
# 连接节点
G.add_edge("中心主题", "分支1")
G.add_edge("中心主题", "分支2")
G.add_edge("中心主题", "分支3")
G.add_edge("分支1", "子分支1.1")
G.add_edge("分支1", "子分支1.2")
# 设置布局
G.layout(prog='dot')
# 保存为图片
G.draw('mind_map.png')
create_mind_map() 方法三:使用 matplotlib 手动绘制 import matplotlib.pyplot as plt
import matplotlib.patches as patches
def create_mind_map():
fig, ax = plt.subplots(figsize=(10, 8))
# 中心节点
center = patches.Circle((0.5, 0.8), 0.05, fc='lightblue', ec='black')
ax.add_patch(center)
plt.text(0.5, 0.8, '中心主题', ha='center', va='center')
# 一级分支
branches = [
(0.3, 0.6, '分支1'),
(0.5, 0.6, '分支2'),
(0.7, 0.6, '分支3')
]
# 绘制一级分支
for x, y, text in branches:
rect = patches.Rectangle((x-0.08, y-0.03), 0.16, 0.06, fc='lightgreen', ec='black')
ax.add_patch(rect)
plt.text(x, y, text, ha='center', va='center')
plt.plot([0.5, x], [0.75, y+0.03], 'k-')
# 二级分支
sub_branches = [
(0.2, 0.4, '子分支1.1'),
(0.4, 0.4, '子分支1.2')
]
# 绘制二级分支
for x, y, text in sub_branches:
rect = patches.Rectangle((x-0.08, y-0.03), 0.16, 0.06, fc='yellow', ec='black')
ax.add_patch(rect)
plt.text(x, y, text, ha='center', va='center')
plt.plot([0.3, x], [0.57, y+0.03], 'k-')
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
ax.axis('off')
plt.savefig('mind_map_matplotlib.png', dpi=300, bbox_inches='tight')
plt.close()
create_mind_map() 方法四:使用专业思维导图库 - mindmaps # 需要先安装:pip install mindmaps
from mindmaps import MindMap
def create_mind_map():
mm = MindMap()
# 添加节点
root = mm.add_node("中心主题")
branch1 = root.add_child("分支1")
branch2 = root.add_child("分支2")
branch3 = root.add_child("分支3")
branch1.add_child("子分支1.1")
branch1.add_child("子分支1.2")
branch2.add_child("子分支2.1")
# 导出为图片
mm.save_as_image("mind_map_custom.png")
create_mind_map() 方法五:使用 markdown 转思维导图(通过 mermaid) def generate_mermaid_mindmap():
mermaid_code = """
```mermaid
mindmap
root((中心主题))
分支1
子分支1.1
子分支1.2
分支2
子分支2.1
分支3 """
with open("mindmap.md", "w") as f:
f.write(mermaid_code)
print("Mermaid格式思维导图已生成,可使用支持mermaid的Markdown查看器渲染") generate_mermaid_mindmap()
## 安装依赖 对于上述方法,可能需要安装以下依赖:
pip install graphviz pygraphviz matplotlib mindmaps```
注意:
graphviz 和 pygraphviz 需要系统安装 Graphviz 软件对于更复杂的思维导图,可以考虑使用专业库如 mindmaps 或 xmind SDK方法五生成的 mermaid 代码需要支持 mermaid 的 Markdown 查看器(如 Typora、VS Code 插件等)才能渲染为图片选择哪种方法取决于你的具体需求:
简单快速:graphviz高度自定义:matplotlib专业功能:mindmaps 或 xmind SDK与文档集成:mermaid网友回复


