圆环图是饼图的修改版,中间区域被切掉。圆环图更关注使用弧的面积来以最有效的方式表示信息,而不是饼图,饼图更关注比较切片之间的比例面积。圆环图在空间方面更有效,因为圆环图内部的空白空间可用于显示有关圆环图的一些附加信息。
作为一个圆环图,它必须是一个饼图。如果我们看饼图,我们将关注图表的中心。另一方面,圆环图消除了比较切片的大小或面积的需要,并将焦点转移到弧的长度上,这反过来又很容易测量。
创建简单的圆环图
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()
自定义圆环图
将图例添加到圆环图
图表图例通常以方框的形式出现在图表的右侧或左侧。它包含图表上每种颜色的小样本以及图表中每种颜色含义的简短描述。
要添加图例,我们只需编写以下代码。
plt.legend(labels, loc = "upper right")
这里plt.legend()有两个参数,第一个是labels,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()
在圆环图的图例框中添加标题
我们可以通过编写以下代码向圆环图中的图例框添加标题:
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()
案例
考虑另一种情况,你必须准备一份不同学生在测试中获得的分数报告,并通过使用圆环图来可视化他们的表现。为了解决这个问题,我们将使用Python的matplotlib库。我们的想法是,我们将制作一个不同学生的名字列表和另一个他们各自的分数列表,并用这个列表制作一个圆环图。
# 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()