首页 > 编程语言 >python绘制多级饼图(分层饼图)

python绘制多级饼图(分层饼图)

时间:2024-05-26 10:00:15浏览次数:17  
标签:labels outer sizes python 多级 colors 分层 inner ax

python绘制多级饼图(分层饼图)

介绍

多级饼图展示了数据的层次结构,其中每个级别表示数据的一个层次。我们可以使用matplotlib绘制多级饼图。

效果

在这里插入图片描述

代码

import matplotlib.pyplot as plt

# 示例数据
outer_labels = ['Category A', 'Category B', 'Category C']
outer_sizes = [30, 45, 25]
outer_colors = ['#ff9999','#66b3ff','#99ff99']

inner_labels = ['A1', 'A2', 'A3', 'B1', 'B2', 'C1', 'C2', 'C3']
inner_sizes = [10, 12, 8, 20, 25, 10, 10, 5]
inner_colors = ['#ff6666','#ff9999','#ffcccc', '#66b3ff','#99ccff', '#99ff99','#66ff66','#33cc33']

# 创建一个图形对象
fig, ax = plt.subplots()

# 绘制外层饼图
ax.pie(outer_sizes, labels=outer_labels, colors=outer_colors, radius=1, wedgeprops=dict(width=0.3, edgecolor='w'))

# 绘制内层饼图
ax.pie(inner_sizes, labels=inner_labels, colors=inner_colors, radius=0.7, wedgeprops=dict(width=0.3, edgecolor='w'))

# 设置绘图区域为正方形,确保饼图为圆形
ax.set(aspect="equal")

# 显示图形
plt.show()

标签:labels,outer,sizes,python,多级,colors,分层,inner,ax
From: https://blog.csdn.net/summerriver1/article/details/139194029

相关文章