"""
author:魏振东
data:2019.12.13
func:饼图绘制
"""
import matplotlib.pyplot as plt
import xlrd
x1 = xlrd.open_workbook("软件开发新技术I学生名单.xls")
# 获取sheet
sheet1 = x1.sheet_by_index(0)
dict1={}
for i in range(1, int(sheet1.nrows)):
if sheet1.cell_value(i,5)=='男':
dict1.setdefault('男', []).append(sheet1.cell_value(i,5))
else:
dict1.setdefault('女', []).append(sheet1.cell_value(i, 5))
man = round(len(dict1['男'])/(len(dict1['男'])+len(dict1['女'])),2)
woman = round(len(dict1['女'])/(len(dict1['男'])+len(dict1['女'])),2)
# 显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.figure(figsize=(6,9))
#labels : 每一块饼图外侧显示的说明文字;
labels = [u'男',u'女',]
# x:每一块饼图的比例,为必填项,如果sum(x)>1,会将多出的部分进行均分
sizes = [man,woman]
# colors:数组,可选参数,默认为:None;用来标注每块饼图的matplotlib颜色参数序列。如果为None,将使用当前活动环的颜色
colors = ['pink','lightskyblue']
# explode : 每一块饼图 离开中心距离,默认值为(0,0),就是不离开中心
explode = (0.05,0,)
plt.pie(sizes,explode=explode,labels=labels,colors=colors,autopct='%1.2f%%',shadow=True,labeldistance=0.8,startangle=30,radius=1.3,counterclock=False)
# 将饼图显示为正圆形,plt.axis( )
plt.axis('equal')
plt.title("Male and female")
plt.legend()
plt.show()
来源:CSDN
作者:魏振东
链接:https://blog.csdn.net/wei_zhen_dong/article/details/103586304