You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python在单张图片中绘制多幅测试类用例占比饼图

使用Python Matplotlib生成双饼图方案

核心思路

借助Matplotlib的子图功能,为前两个测试类(TC1、TC2)分别绘制饼图,直观展示自动化与手动测试用例的占比。需先将数据中的字符串类型数值转换为整数,再逐一生成饼图并添加必要标注。

完整代码实现

import matplotlib.pyplot as plt

# 原始测试数据
test_data = {
    'Name': ['TC1', 'TC2', 'TC3'],
    'Automated': ['12', '10', '8'],
    'manual': ['2', '4', '6']
}

# 将字符串类型的计数转为整数
automated_counts = list(map(int, test_data['Automated']))
manual_counts = list(map(int, test_data['manual']))
class_names = test_data['Name']

# 创建1行2列的子图画布
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))

# 定义饼图显示标签
pie_labels = ['自动化测试用例', '手动测试用例']

# 绘制TC1的饼图
tc1_sizes = [automated_counts[0], manual_counts[0]]
ax1.pie(tc1_sizes, labels=pie_labels, autopct='%1.1f%%', startangle=90)
ax1.set_title(f'{class_names[0]} 测试用例占比')
ax1.axis('equal')  # 确保饼图为正圆形

# 绘制TC2的饼图
tc2_sizes = [automated_counts[1], manual_counts[1]]
ax2.pie(tc2_sizes, labels=pie_labels, autopct='%1.1f%%', startangle=90)
ax2.set_title(f'{class_names[1]} 测试用例占比')
ax2.axis('equal')

# 自动调整子图间距,避免内容重叠
plt.tight_layout()
plt.show()

关键细节说明

  • 数据转换:用map(int, ...)把原始字符串格式的计数转为整数,确保占比计算准确。
  • 子图布局:plt.subplots(1,2)生成横向排列的两个子图,分别对应两个测试类的统计结果。
  • 饼图配置:autopct='%1.1f%%'显示百分比数值,startangle=90设置饼图的起始角度,axis('equal')保证饼图形状为正圆。
  • 布局优化:tight_layout()自动调整子图之间的间距,避免标题、标签等内容互相遮挡。

内容的提问来源于stack exchange,提问作者Yugank DUbey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 14:57:29