📜  在Python使用 Matplotlib 绘制圆环图

📅  最后修改于: 2022-05-13 01:55:06.693000             🧑  作者: Mango

在Python使用 Matplotlib 绘制圆环图

先决条件: matplotlib 中的饼图

甜甜圈图是饼图的修改版本,中心区域被切掉。甜甜圈更关心使用弧线的面积以最有效的方式表示信息,而不是饼图,饼图更侧重于比较切片之间的比例面积。圆环图在空间方面更有效,因为圆环图内部的空白空间可用于显示有关圆环图的一些附加信息。

作为一个甜甜圈图,它必须是一个饼图。如果我们查看饼图,我们将关注图表的中心。另一方面,圆环图无需比较切片的大小或面积,并将重点转移到弧的长度上,这反过来又易于测量。

创建一个简单的圆环图

创建圆环图涉及三个简单的步骤,如下所示:

  • 创建饼图
  • 画一个合适尺寸的圆。
  • 在饼图的中心添加圆圈
Python3
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')
  
# Displaing Chart
plt.show()


Python3
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")
  
# Displaing Chart
plt.show()


Python3
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")
  
# Displaing Chart
plt.show()


Python3
# library
import matplotlib.pyplot as plt
  
# list of name of students
names = ['Manish', 'Atul', 'Priya', 'Harshit']
  
# list of their respective marks
marks = [45, 66, 55, 77]
  
# Create a circle at the center of
# the plot
my_circle = plt.Circle((0, 0), 0.7, color='white')
  
# Give color names
plt.pie(marks, labels=names, autopct='%1.1f%%',
        colors=['red', 'green', 'blue', 'yellow'])
  
p = plt.gcf()
p.gca().add_artist(my_circle)
  
# Show the graph
plt.show()


输出:

自定义圆环图

向圆环图添加图例

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

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

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

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

例子:

蟒蛇3

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")
  
# Displaing Chart
plt.show()

输出:



为圆环图中的图例框添加标题

我们可以通过编写以下代码为圆环图中的图例框添加标题:

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

例子:

蟒蛇3

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")
  
# Displaing Chart
plt.show()

输出:

示例 2:考虑另一种情况,您必须准备一份不同学生在测试中获得的分数的报告,并使用圆环图可视化他们的表现。为了解决这个问题,我们将使用Python 的matplotlib 库。这个想法是我们将列出不同学生的姓名和他们各自的分数的另一个列表,并使用此列表制作圆环图。

蟒蛇3

# library
import matplotlib.pyplot as plt
  
# list of name of students
names = ['Manish', 'Atul', 'Priya', 'Harshit']
  
# list of their respective marks
marks = [45, 66, 55, 77]
  
# Create a circle at the center of
# the plot
my_circle = plt.Circle((0, 0), 0.7, color='white')
  
# Give color names
plt.pie(marks, labels=names, autopct='%1.1f%%',
        colors=['red', 'green', 'blue', 'yellow'])
  
p = plt.gcf()
p.gca().add_artist(my_circle)
  
# Show the graph
plt.show()

输出: