编辑代码

import matplotlib.pyplot as plt

# 设置中文显示
plt.rcParams['font.sans-serif'] = ['SimHei']  # 设置默认字体
plt.rcParams['axes.unicode_minus'] = False    # 解决负号显示问题

# 准备数据
sales = [38, 32, 30]
products = ['产品A', '产品B', '产品C']
persons = ['李敏', '王鹏', '张晶']

# 组合标签(产品+负责人)
labels = [f'{p}\n({name})' for p, name in zip(products, persons)]

# 绘制饼图
plt.figure(figsize=(8, 6))
patches, texts, autotexts = plt.pie(
    sales,
    labels=labels,
    autopct='%1.1f%%',
    startangle=90,
    textprops={'fontsize': 12},
    colors=['#ff9999','#66b3ff','#99ff99']
)

# 设置百分比数字样式
for autotext in autotexts:
    autotext.set_color('black')
    autotext.set_fontsize(12)

# 添加标题
plt.title('季度产品销售占比分布', fontsize=16, pad=20)

# 添加图例
plt.legend(
    title="产品负责人",
    loc="upper right",
    bbox_to_anchor=(1.1, 1),
    labels=[f'{p} - {n}' for p, n in zip(products, persons)]
)

# 保持长宽相等
plt.axis('equal')

# 显示图表
plt.show()