python数据分析_绘制饼图,直方图

从前面爬去的CQU就业数据,进行简单分类,绘制饼图,和直方图

# -*- coding: utf-8 -*-
#!/usr/bin/python
import matplotlib.pyplot as plt
import openpyxl
from wordcloud import WordCloud
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']

def get_job_data(grad_year, college):
    """"从excl表中获取指定学院数据"""
    wb = openpyxl.load_workbook(grad_year+ '重庆大学毕业生就业单位.xlsx')
    sheet = wb[college]
    max_row = sheet.max_row
    data = {}
    for i in range(1, max_row + 1):
        company = sheet["A" + str(i)].value
        number = sheet["B" + str(i)].value
        data[company] = number
    return data


def enrolment_rate(data):
    """统计升学和就业人数"""
    school_amount = 0
    company_amount = 0
    for key in data:
        if "大学" in key:
            school_amount += data[key]
        elif "研究所" in key:
            school_amount += data[key]
        else:
            company_amount += data[key]
    return school_amount,company_amount

def get_school_number(data):
    """获取升学学生信息"""
    school = {}
    work={}
    for key in data:
        if "大学" in key:
            school[key] = data[key]
        elif "研究所" in key:
            school[key] = data[key]
        else:
            work[key] = data[key]
    return school,work




def plot_histogram(college):
    """绘制给定学院升学就业直方图"""
    school = []
    company = []
    for i in range(2014, 2018):
        data = get_job_data(str(i), college)
        school_amount, company_amount = enrolment_rate(data)
        school.append(school_amount)
        company.append(company_amount)
    name_list = ['2014', '2015', '2016', '2017']
    x = list(range(len(school)))
    total_width, n = 0.8, 2
    width = total_width / n
    a = plt.bar(x, school, width=width, label='升学', fc='r')
    autolabel(a)
    for i in range(len(x)):
        x[i] = x[i] + width
    b = plt.bar(x, company, width=width, label='就业', tick_label=name_list, fc='b')
    autolabel(b)
    plt.legend()
    plt.title(college+"升学就业人数")
    plt.savefig(college+'升学就业直方图.png', dpi=300)


def plot_pie(grad_year, college, type,flag ):
    """画给定学院,给定毕业年份,升学或就业的饼图"""
    # 饼图的大小,高宽
    plt.figure(figsize=(10, 6))
    # 定义饼图的标签
    labels = list(type.keys())
    sizes = list(type.values())
    patches, l_text, p_text = plt.pie(sizes,labels=labels,labeldistance=1.1,
                                      autopct="%3.1f%%",shadow=False,startangle=90,
                                      pctdistance=0.6)
    # 设置x/y轴坐标一致
    plt.axis("equal")
    # 标题
    if flag == 1:
       c = "升学"
    else:
       c = "就业"
    plt.title(grad_year+college+c+"单位统计")
    plt.savefig(grad_year+college+c+'单位饼图.png', dpi=300)
    plt.close()


def do_plot_pie(grad_year, college):
    """查找升学或就业的数据,并画饼图"""
    data = get_job_data(grad_year, college)
    school, work = get_school_number(data)
    plot_pie(grad_year, college, school,1)
    plot_pie(grad_year, college, work, 0)


def autolabel(rects):
    """为直方图显示y轴数据"""
    for rect in rects:
        height = rect.get_height()
        plt.text(rect.get_x() + rect.get_width() / 2., 1.01 * height, "%s" % int(height))

if __name__ == '__main__':
    for year in range(2014,2019):
        do_plot_pie(str(year), "物理学院")

你可能感兴趣的:(算法)