使用 Python Matplotlib 绘制甜甜圈图

前置知识:Matplotlib 饼图

甜甜圈图本质上是切掉了中心区域的饼图的改良版本。与侧重于比较各切片之间比例面积的饼图不同,甜甜圈图更侧重于利用圆弧的面积来以最有效的方式呈现信息。甜甜圈图在空间利用上更加高效,因为图表内部的空白区域可以用来展示一些关于该图表的附加信息。

要制作一个甜甜圈图,它本质上必须首先是一个饼图。如果我们观察饼图,我们的注意力通常会集中在图表的中心。另一方面,甜甜圈图消除了比较切片大小或面积的需要,将焦点转移到了圆弧的长度上,这使得数据更容易被衡量。

创建一个简单的甜甜圈图

创建甜甜圈图包含以下三个简单的步骤:

  • 创建一个饼图
  • 绘制一个具有合适尺寸的圆
  • 将该圆添加到饼图的中心
import matplotlib.pyplot as plt

# Setting labels for items in Chart
Employee = [‘Roshni‘, ‘Shyam‘, ‘Priyanshi‘,
            ‘Harshit‘, ‘Anmol‘]

# Setting size in Chart based on 
# given values
Salary = [40000, 50000, 70000, 54000, 44000]

# colors
colors = [‘#FF0000‘, ‘#0000FF‘, ‘#FFFF00‘, 
          ‘#ADFF2F‘, ‘#FFA500‘]
# explosion
explode = (0.05, 0.05, 0.05, 0.05, 0.05)

# Pie Chart
plt.pie(Salary, colors=colors, labels=Employee,
        autopct=‘%1.1f%%‘, pctdistance=0.85,
        explode=explode)

# draw circle
centre_circle = plt.Circle((0, 0), 0.70, fc=‘white‘)
fig = plt.gcf()

# Adding Circle in Pie chart
fig.gca().add_artist(centre_circle)

# Adding Title of chart
plt.title(‘Employee Salary Details‘)

# Displaying Chart
plt.show()

输出:

!image

自定义甜甜圈图

为甜甜圈图添加图例

图例通常以框的形式出现在图表的右侧或左侧。它包含图表上每种颜色的小样本,以及关于每种颜色在图表中含义的简短描述。

要添加图例,我们只需编写以下代码。

plt.legend(labels, loc = "upper right")

这里 plt.legend() 接受两个参数,第一个是标签,loc 用于设置图例框的位置。

示例:

import matplotlib.pyplot as plt

# Setting size in Chart based on 
# given values
sizes = [100, 500, 70, 54, 440]

# Setting labels for items in Chart
labels = [‘Apple‘, ‘Banana‘, ‘Mango‘, ‘Grapes‘, ‘Orange‘]

# colors
colors = [‘#FF0000‘, ‘#0000FF‘, ‘#FFFF00‘, ‘#ADFF2F‘, ‘#FFA500‘]

# explosion
explode = (0.05, 0.05, 0.05, 0.05, 0.05)

# Pie Chart
plt.pie(sizes, colors=colors, labels=labels,
        autopct=‘%1.1f%%‘, pctdistance=0.85, 
        explode=explode)

# draw circle
centre_circle = plt.Circle((0, 0), 0.70, fc=‘white‘)
fig = plt.gcf()

# Adding Circle in Pie chart
fig.gca().add_artist(centre_circle)

# Adding Title of chart
plt.title(‘Favourite Fruit Survey‘)

# Add Legends
plt.legend(labels, loc="upper right")

# Displaying Chart
plt.show()

输出:

!image

为甜甜圈图中的图例框添加标题

我们可以通过编写以下代码为甜甜圈图中的图例框添加一个标题:

plt.legend(labels, loc = "upper right",title="Fruits Color")

示例:

import matplotlib.pyplot as plt

# Setting size in Chart based on 
# given values
sizes = [100, 500, 70, 54, 440]

# Setting labels for items in Chart
labels = [‘Apple‘, ‘Banana‘, ‘Mango‘, ‘Grapes‘,
          ‘Orange‘]

# colors
colors = [‘#FF0000‘, ‘#0000FF‘, ‘#FFFF00‘, ‘#ADFF2F‘,
          ‘#FFA500‘]

# explosion
explode = (0.05, 0.05, 0.05, 0.05, 0.05)

# Pie Chart
plt.pie(sizes, colors=colors, labels=labels,
        autopct=‘%1.1f%%‘, pctdistance=0.85,
        explode=explode)

# draw circle
centre_circle = plt.Circle((0, 0), 0.70, fc=‘white‘)
fig = plt.gcf()

# Adding Circle in Pie chart
fig.gca().add_artist(centre_circle)

# Adding Title of chart
plt.title(‘Favourite Fruit Survey‘)

# Add Legends
plt.legend(labels, loc="upper right", title="Fruits Color")

# Displaying Chart
plt.show()

输出:

!image

示例 2: 让我们考虑另一种情况,你需要准备一份关于不同学生在测试中获得的分数的报告,并使用甜甜圈图可视化他们的表现。为了解决这个问题,我们将使用 Python 的 matplotlib 库。我们的思路是制作一个包含不同学生姓名的列表,以及另一个包含他们各自分数的列表,然后使用这些列表来制作图表。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。如需转载,请注明文章出处豆丁博客和来源网址。https://shluqu.cn/37403.html
点赞
0.00 平均评分 (0% 分数) - 0