饼图用于展示各类别占总体的比例,每个扇区的大小代表该类别的占比。
Matplotlib 的 plt.pie() 函数提供了丰富的定制选项,可以轻松创建从基础到高级的饼图。
最简单的饼图只需要传入一组数值,默认会按照数值大小分配扇区:
import matplotlib.pyplot as plt
# 数据:各产品的销售额占比
sizes = [35, 25, 20, 15, 5]
labels = ['产品A', '产品B', '产品C', '产品D', '其他']
plt.pie(sizes, labels=labels)
plt.title('基础饼图 - 销售额占比')
plt.show()
labels 指定扇区标签,默认从(0°)开始逆时针排列。使用 autopct 参数在扇区内显示百分比:
sizes = [35, 25, 20, 15, 5]
labels = ['产品A', '产品B', '产品C', '产品D', '其他']
plt.pie(sizes, labels=labels, autopct='%1.1f%%') # 显示一位小数的百分比
plt.title('显示百分比的饼图')
plt.show()
autopct 可以是格式字符串或函数。pie() 支持丰富的样式参数:
| 参数 | 说明 | 示例 |
|---|---|---|
colors | 扇区颜色序列 | colors=['#ff9999','#66b3ff','#99ff99','#ffcc99','#c2c2f0'] |
startangle | 第一个扇区的起始角度(度数) | startangle=90 (从顶部开始) |
shadow | 是否显示阴影 | shadow=True |
explode | 突出显示某些扇区,传入与数据长度相同的序列,数值表示突出距离 | explode=[0.1, 0, 0, 0, 0] (突出第一个扇区) |
labeldistance | 标签距离圆心的比例 | labeldistance=1.1 |
pctdistance | 百分比文字距离圆心的比例 | pctdistance=0.6 |
sizes = [35, 25, 20, 15, 5]
labels = ['产品A', '产品B', '产品C', '产品D', '其他']
colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99', '#c2c2f0']
explode = [0.1, 0, 0, 0, 0] # 突出产品A
plt.pie(sizes, labels=labels, colors=colors, explode=explode,
autopct='%1.1f%%', startangle=90, shadow=True)
plt.title('定制样式的饼图')
plt.show()
使用 explode 参数可以将某些扇区拉出,强调重点:
sizes = [35, 25, 20, 15, 5]
labels = ['产品A', '产品B', '产品C', '产品D', '其他']
explode = [0.1, 0.05, 0, 0, 0] # 产品A突出0.1,产品B突出0.05
plt.pie(sizes, labels=labels, explode=explode, autopct='%1.1f%%', shadow=True)
plt.title('突出显示扇区')
plt.show()
explode 值越大,扇区离圆心越远。通过在饼图中心添加一个白色圆盘可以制作环形图,更简洁美观:
sizes = [35, 25, 20, 15, 5]
labels = ['产品A', '产品B', '产品C', '产品D', '其他']
colors = ['#ff9999', '#66b3ff', '#99ff99', '#ffcc99', '#c2c2f0']
# 绘制饼图
plt.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
# 添加中心圆
centre_circle = plt.Circle((0,0), 0.70, fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)
plt.title('环形图')
plt.show()
plt.Circle 在中心添加白色圆形。通过叠加两个不同半径的饼图,展示层级数据:
# 数据:大类与小类
group_sizes = [60, 40] # 大类占比
group_labels = ['类别A', '类别B']
sub_sizes = [30, 20, 10, 25, 15] # 小类占比(需对应大类细分)
sub_labels = ['A1', 'A2', 'A3', 'B1', 'B2']
fig, ax = plt.subplots()
# 内层饼图(大类)
ax.pie(group_sizes, labels=group_labels, radius=1, autopct='%1.0f%%',
pctdistance=0.8, labeldistance=1.1, colors=['#ff9999', '#66b3ff'])
# 外层饼图(小类)
ax.pie(sub_sizes, labels=sub_labels, radius=0.7, autopct='%1.0f%%',
pctdistance=0.6, labeldistance=1.2, colors=['#ffcccc', '#ffb3b3', '#ff9999', '#b3d9ff', '#99c2ff'])
ax.set_title('嵌套饼图 - 多级占比')
plt.show()
radius 参数控制饼图半径,小半径覆盖在大半径之上。使用 subplots 创建多个饼图进行比较:
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
# 第一季度销售占比
sizes1 = [40, 30, 20, 10]
labels1 = ['产品A', '产品B', '产品C', '产品D']
axes[0].pie(sizes1, labels=labels1, autopct='%1.1f%%')
axes[0].set_title('第一季度')
# 第二季度销售占比
sizes2 = [30, 25, 25, 20]
labels2 = ['产品A', '产品B', '产品C', '产品D']
axes[1].pie(sizes2, labels=labels2, autopct='%1.1f%%')
axes[1].set_title('第二季度')
plt.tight_layout()
plt.show()
pie() 返回三个对象:wedges (扇区对象), texts (标签文本), autotexts (百分比文本),可用于进一步定制:
sizes = [35, 25, 20, 15, 5]
labels = ['产品A', '产品B', '产品C', '产品D', '其他']
wedges, texts, autotexts = plt.pie(sizes, labels=labels, autopct='%1.1f%%')
# 设置百分比文字颜色为白色
for autotext in autotexts:
autotext.set_color('white')
plt.title('定制百分比文字颜色')
plt.show()
默认情况下,饼图可能显示为椭圆。添加 plt.axis('equal') 或 ax.axis('equal') 可使饼图为正圆:
plt.pie(sizes, labels=labels, autopct='%1.1f%%')
plt.axis('equal') # 确保饼图为圆形
plt.title('正圆饼图')
plt.show()
import matplotlib.pyplot as plt
# 数据
sizes = [38, 27, 18, 12, 5]
labels = ['市场营销', '产品研发', '行政管理', '客户支持', '其他']
colors = ['#2ecc71', '#3498db', '#9b59b6', '#f1c40f', '#e74c3c']
explode = (0.05, 0, 0, 0, 0) # 突出市场营销
fig, ax = plt.subplots(figsize=(8, 8))
wedges, texts, autotexts = ax.pie(sizes, labels=labels, colors=colors,
explode=explode, autopct='%1.1f%%',
startangle=90, shadow=True,
textprops={'fontsize': 12})
# 设置百分比文字颜色和样式
for autotext in autotexts:
autotext.set_color('white')
autotext.set_fontweight('bold')
ax.axis('equal') # 正圆
ax.set_title('2023年度公司支出构成', fontsize=16, pad=20)
plt.tight_layout()
plt.show()
使用 savefig() 保存:
plt.savefig('pie_chart.png', dpi=300, bbox_inches='tight') # PNG
plt.savefig('pie_chart.pdf', bbox_inches='tight') # 矢量PDF
饼图是展示占比数据的直观选择,掌握突出显示、环形图、嵌套饼图等技巧,能让你的数据故事更加生动。下一章我们将学习箱线图,用于展示数据的分布和异常值。