百度飞桨小白逆袭第三天

百度飞桨小白逆袭第三天题目解析

!!!作业说明!!!
请在下方提示位置,补充代码,对《青春有你2》对选手体重分布进行可视化,绘制饼状图,如下图所示:
不要求跟下图样式一模一样
在这里插入图片描述
任务描述:
基于第二天实践使用Python来爬去百度百科中《青春有你2》所有参赛选手的信息,进行数据可视化分析。
在这里插入图片描述
在这里插入图片描述

下面开始演示代码:

一、绘制选手区域分布柱状图

import matplotlib.pyplot as plt
import numpy as np 
import json
import matplotlib.font_manager as font_manager

#显示matplotlib生成的图形
%matplotlib inline

with open('data/data31557/20200422.json', 'r', encoding='UTF-8') as file:
         json_array = json.loads(file.read())

#绘制小姐姐区域分布柱状图,x轴为地区,y轴为该区域的小姐姐数量

zones = []
for star in json_array:
    zone = star['zone']
    zones.append(zone)
print(len(zones))
print(zones)


zone_list = []
count_list = []

for zone in zones:
    if zone not in zone_list:
        count = zones.count(zone)
        zone_list.append(zone)
        count_list.append(count)

print(zone_list)
print(count_list)

# 设置显示中文
plt.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体

plt.figure(figsize=(20,15))

plt.bar(range(len(count_list)), count_list,color='r',tick_label=zone_list,facecolor='#9999ff',edgecolor='white')

# 这里是调节横坐标的倾斜度,rotation是度数,以及设置刻度字体大小
plt.xticks(rotation=45,fontsize=20)
plt.yticks(fontsize=20)

plt.legend()
plt.title('''《青春有你2》参赛选手''',fontsize = 24)
plt.savefig('/home/aistudio/work/result/bar_result.jpg')
plt.show()

二、请在下面完成作业,对选手体重分布进行可视化,绘制饼状图(官方提示)

with open('data/data31557/20200422.json', 'r', encoding='UTF-8') as file:
         json_array = json.loads(file.read())

weights = []
for star in json_array:
    weight = star['weight']
    weights.append(weight)

len45 = []
len50 = []
len54 = []
len55 = []

for i,weight in enumerate(weights):
    if int(weight[:2]) < 45:
        len45.append(i)
    elif 45 <= int(weight[:2]) < 50:
        len50.append(i)
    elif 50 <= int(weight[:2]) < 55:
        len54.append(i)
    else:
        len55.append(i)

ra45 = len(len45)/109
ra50 = len(len50)/109
ra54 = len(len54)/109
ra55 = len(len55)/109

weight_list = ["<45","45~50","50~55",">55"]
count_list = [ra45 ,ra50 ,ra54 ,ra55]


plt.figure(figsize=(20,15))


plt.pie(x= count_list, labels=weight_list, shadow = True, textprops = {'fontsize':20, 'color':'k'} ,explode = [0.1,0.1,0.1,0.2], autopct='%.1f%%', pctdistance =0.6 , labeldistance = 1.2) 
plt.title('''《青春有你2》参赛选手体重分布情况''',fontsize = 24)


#plt.bar(range(len(count_list)), count_list,color='r',tick_label=weight_list,facecolor='#9999ff',edgecolor='white')


plt.rcParams['font.sans-serif'] = ['SimHei'] 

plt.savefig("《青春有你2》参赛选手体重分布情况.jpg")
plt.axis('equal')
plt.legend()
plt.show() 

最终运行结果如下:
在这里插入图片描述

在这里插入图片描述
以上便是这次作业的全部内容,若有疑问欢迎评论留言,谢谢!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值