使用Matplotlib绘制圆环图

本文介绍了如何使用Python的Matplotlib库创建圆环图,一种强调弧长而非面积比较的图表类型,用于有效地展示信息。文中展示了如何添加图例、自定义颜色和在特定情境下如学生分数报告中应用圆环图。
摘要由CSDN通过智能技术生成

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

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

创建简单的圆环图

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()

在这里插入图片描述

  • 10
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值