

复合饼图是兼顾整体占比与局部细节的专业数据可视化图表,核心解决普通饼图无法同时展示「大类构成」和「重点子类细分」的问题。其核心实现逻辑:将需要重点分析的目标大类从主饼图中分离突出,在其外侧同轴绘制环形图,精准展示该大类下的子类比例;环图与突出扇形共用同一圆心,保证视觉连贯性与数据关联性。该图表适用场景广泛,尤其适合生物信息、成分分析、财务统计等需要总览全局 + 聚焦细节的占比数据展示。

# 测试数据
main_data={
"Phospholipids":100,
"Fatty_acids":80,
"Amino_acids":45,
"Saccharides":30
}
sub_data={
"Phosphatidylcholine":70,
"Phosphatidylethanolamine":45,
"Phosphatidylserine":20,
"Phosphatidylinositol":15
}
target_category = "Phospholipids"
title = "Lipidomics Classification Distribution"
mian_data与sub_data示例数据如下:



1. 导入库
导入绘图、数据处理所需的核心库,无需额外安装第三方工具,基础 Python 数据分析环境即可运行。
# 导入库
import os
import pandas as pd
from collections import Counter
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Wedge
import itertools
2.颜色映射
提取主、子数据的标签和数值,配置统一的配色方案,保证图表色彩协调、专业美观。
# 准备数据标签
main_labels = list(main_data.keys())
main_sizes = list(main_data.values())
sub_labels = list(sub_data.keys())
sub_sizes = list(sub_data.values())
# 颜色映射
all_labels = main_labels + sub_labels
colors_palette = [
"#b86b8f", "#e8a8b8", "#e0c4af", "#dcdc9e", "#b3e2b3",
"#bce8dd", "#87b8e6", "#8a8cd1", "#728aac", "#bb7fdb"
]
color_map = dict(zip(sorted(all_labels), itertools.cycle(colors_palette)))
main_colors = [color_map[l] for l in main_labels]
sub_colors = [color_map[l] for l in sub_labels]
3.主饼图绘制
绘制一级分类整体占比饼图,自动定位目标分类并向外偏移突出。同时统一科研绘图字体与样式参数,保留所有扇形的角度、圆心数据,为后续叠加子类环图提供位置支撑
# 计算 explode(突出目标大类)
try:
gp_idx = main_labels.index(target_category)
explode = [0.15if i == gp_idx else0for i inrange(len(main_labels))]
except ValueError:
gp_idx = 0
explode = [0] * len(main_labels)
# 绘图字体与样式设置
plt.rcParams['pdf.fonttype'] = 42
plt.rcParams['ps.fonttype'] = 42
plt.rcParams['font.family'] = 'Times New Roman'
fig, ax = plt.subplots(figsize=(12, 7))
# 绘制主饼图
patches, texts, autotexts = ax.pie(
main_sizes, labels=main_labels, autopct='%1.1f%%',
textprops={'fontsize': 8}, startangle=10, explode=explode,
colors=main_colors, counterclock=False, pctdistance=0.75,
wedgeprops={'edgecolor': 'white', 'linewidth': 1.5, 'alpha': 1}
)
效果如下:

4.子类环图绘制
环图必须绑定突出扇形的移动后圆心,然后获取目标扇形的圆心、角度、半径参数,基于该参数绘制环形图,完美贴合主饼图,自动添加子类标签和百分比,小比例分类自动隐藏避免重叠。
# 获取被突出扇形的参数(关键:圆心跟着扇形移动)
gp_patch = patches[gp_idx]
theta1, theta2 = gp_patch.theta1, gp_patch.theta2
center = gp_patch.center # ← 移动后的圆心
radius = gp_patch.r
# 绘制子类环图(围绕同一个移动后的圆心)
current_theta = theta1
total_sub = sum(sub_sizes)
for size, color, label inzip(sub_sizes, sub_colors, sub_labels):
angle_width = (size / total_sub) * (theta2 - theta1)
wedge = Wedge(
center, radius + 0.35, current_theta, current_theta + angle_width,
width=0.25, facecolor=color, edgecolor='white', linewidth=0.8, alpha=1
)
ax.add_patch(wedge)
# 显示标签(坐标也基于移动后的圆心)
if size > total_sub * 0.02:
mid_angle = current_theta + angle_width / 2
x = center[0] + (radius + 0.45) * np.cos(np.deg2rad(mid_angle))
y = center[1] + (radius + 0.45) * np.sin(np.deg2rad(mid_angle))
sub_pct = size / total_sub * 100
ax.text(x, y, f"{label}\n{sub_pct:.1f}%", ha='center', va='center', fontsize=7)
current_theta += angle_width
效果如下:

5.图例修饰与导出
添加标题、图例,优化布局,支持高清 PDF 格式导出,直接用于报告、论文:
# 修饰
total_n = sum(main_sizes)
ax.set_title(f"{title} (n={total_n})", fontsize=16,fontweight='bold')
ax.axis('equal')
# 图例
handles = patches + [Wedge((0,0), 0, 0, 0, facecolor=c) for c in sub_colors]
labels = main_labels + sub_labels
ax.legend(handles, labels, loc="center left", bbox_to_anchor=(1.25, 0.5), frameon=False)
plt.tight_layout()
plt.savefig(f"Pieplot-Class.pdf", bbox_inches='tight', pad_inches=0.1)
plt.show()



本教程基于 Python 的matplotlib库,实现了主饼图 + 子类环图的复合饼图绘制,完整覆盖数据准备、绘图、美化、导出全流程。通过本教程,你可以快速掌握复合饼图的核心绘制逻辑,无需依赖复杂工具,用 Python 即可实现高质量的层级化占比可视化,高效展示数据的全局特征与局部细节。
欧易生物简介
Oebiotech
欧易生物是一家致力于为生命科学研究提供多组学技术的研究服务机构,产品涵盖单细胞及时空多组学、基因组学、转录组学、表观组学、蛋白组学、代谢组学、生物信息学以及临床诊断产品开发,秉承「以生物科技 成就他人 造福大众」的企业使命,用技术改变生活,用科技造福人类。

欧易生物先后与中国海洋大学、中国科学院遗传与发育生物学研究所等机构建立了紧密的产学研合作,与日立诊断产品有限公司共建联合研发实验室,与华东师范大学合作建立院士专家工作站,并陆续荣获国家级专精特新“小巨人”、上海市科技小巨人企业、上海市专利试点企业、上海市企业技术中心、闵行区研发机构、闵行区科技小巨人企业等资质。还获得知识产权管理体系认证企业资质,总授权发明专利53项、在审发明专利54项、授权软件著作权213项(含欧易生物及旗下子公司,截止到2025年12月)。至今已累计助力客户发表6000+高水平研究论文,累计影响因子40000+;发文期刊包括Nature、Cell、Science、Cancer Discovery、Cell Discovery 等知名期刊。
欢迎关注欧易生物质谱公众号
获取更多干货内容
往期推荐:
END
撰稿:Jizhe.Han
