在Python中如何绘制带有连接线的双饼图

发布时间:2023-05-11 11:04:39 作者:iii
来源:亿速云 阅读:84

在Python中如何绘制带有连接线的双饼图

在数据可视化中,饼图是一种常用的图表类型,用于展示各部分在整体中的占比。然而,当我们需要比较两个相关数据集时,单一的饼图可能无法满足需求。此时,双饼图(也称为双层饼图或嵌套饼图)便成为一种有效的解决方案。本文将介绍如何在Python中使用Matplotlib库绘制带有连接线的双饼图。

1. 准备工作

在开始之前,确保你已经安装了Python和Matplotlib库。如果尚未安装,可以使用以下命令进行安装:

pip install matplotlib

2. 导入必要的库

首先,我们需要导入Matplotlib库中的pyplot模块,以及用于处理数据的numpy库。

import matplotlib.pyplot as plt
import numpy as np

3. 准备数据

假设我们有两个数据集,分别表示两个不同年份的销售数据。每个数据集包含四个类别的销售额。

# 数据准备
categories = ['Category A', 'Category B', 'Category C', 'Category D']
sales_2022 = [30, 25, 20, 25]
sales_2023 = [35, 20, 15, 30]

4. 绘制双饼图

接下来,我们将绘制两个饼图,并将它们连接起来。首先,我们创建一个图形对象,并设置其大小。

fig, ax = plt.subplots(figsize=(10, 5))

4.1 绘制第一个饼图

我们使用ax.pie()方法绘制第一个饼图,并设置其位置为左侧。

# 绘制第一个饼图
wedges1, texts1, autotexts1 = ax.pie(sales_2022, labels=categories, autopct='%1.1f%%', startangle=90, radius=1.2)

4.2 绘制第二个饼图

同样地,我们绘制第二个饼图,并将其位置设置为右侧。

# 绘制第二个饼图
wedges2, texts2, autotexts2 = ax.pie(sales_2023, labels=categories, autopct='%1.1f%%', startangle=90, radius=0.8)

4.3 添加连接线

为了在两个饼图之间添加连接线,我们需要计算每个类别的起始和结束角度,并在它们之间绘制线条。

# 计算连接线的起始和结束角度
theta1 = np.deg2rad(np.cumsum(sales_2022) - sales_2022 / 2)
theta2 = np.deg2rad(np.cumsum(sales_2023) - sales_2023 / 2)

# 绘制连接线
for i in range(len(categories)):
    x1 = 1.2 * np.cos(theta1[i])
    y1 = 1.2 * np.sin(theta1[i])
    x2 = 0.8 * np.cos(theta2[i])
    y2 = 0.8 * np.sin(theta2[i])
    ax.plot([x1, x2], [y1, y2], color='black', linestyle='-', linewidth=1)

5. 美化图表

为了使图表更加美观,我们可以添加标题、调整字体大小等。

# 添加标题
ax.set_title('Sales Comparison: 2022 vs 2023', fontsize=16)

# 调整字体大小
for text in texts1 + texts2:
    text.set_fontsize(12)
for autotext in autotexts1 + autotexts2:
    autotext.set_fontsize(10)

6. 显示图表

最后,我们使用plt.show()方法显示图表。

plt.show()

7. 完整代码

以下是完整的代码示例:

import matplotlib.pyplot as plt
import numpy as np

# 数据准备
categories = ['Category A', 'Category B', 'Category C', 'Category D']
sales_2022 = [30, 25, 20, 25]
sales_2023 = [35, 20, 15, 30]

# 创建图形对象
fig, ax = plt.subplots(figsize=(10, 5))

# 绘制第一个饼图
wedges1, texts1, autotexts1 = ax.pie(sales_2022, labels=categories, autopct='%1.1f%%', startangle=90, radius=1.2)

# 绘制第二个饼图
wedges2, texts2, autotexts2 = ax.pie(sales_2023, labels=categories, autopct='%1.1f%%', startangle=90, radius=0.8)

# 计算连接线的起始和结束角度
theta1 = np.deg2rad(np.cumsum(sales_2022) - sales_2022 / 2)
theta2 = np.deg2rad(np.cumsum(sales_2023) - sales_2023 / 2)

# 绘制连接线
for i in range(len(categories)):
    x1 = 1.2 * np.cos(theta1[i])
    y1 = 1.2 * np.sin(theta1[i])
    x2 = 0.8 * np.cos(theta2[i])
    y2 = 0.8 * np.sin(theta2[i])
    ax.plot([x1, x2], [y1, y2], color='black', linestyle='-', linewidth=1)

# 添加标题
ax.set_title('Sales Comparison: 2022 vs 2023', fontsize=16)

# 调整字体大小
for text in texts1 + texts2:
    text.set_fontsize(12)
for autotext in autotexts1 + autotexts2:
    autotext.set_fontsize(10)

# 显示图表
plt.show()

8. 总结

通过本文的介绍,我们学习了如何在Python中使用Matplotlib库绘制带有连接线的双饼图。这种图表类型非常适合用于比较两个相关数据集,并且通过连接线可以清晰地展示各部分之间的关系。希望本文对你有所帮助,祝你在数据可视化的道路上越走越远!

推荐阅读:
  1. python中怎么使用字典dict函数
  2. dir函数中python中的使用方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:Python如何用PsUtil实现实时监控系统状态

下一篇:Python内置logging如何使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》