python-《青春有你2》对选手体重分布进行数据可视化

# 下载中文字体
!wget https://mydueros.cdn.bcebos.com/font/simhei.ttf
# 将字体文件复制到matplotlib字体路径
!cp simhei.ttf /opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/ttf/
# 一般只需要将字体文件复制到系统字体目录下即可,但是在aistudio上该路径没有写权限,所以此方法不能用
# !cp simhei.ttf /usr/share/fonts/

# 创建系统字体文件路径
!mkdir .fonts
# 复制文件到该路径
!cp simhei.ttf .fonts/
!rm -rf .cache/matplotlib

绘制选手区域分布柱状图

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

效果:
109
[‘中国湖北’, ‘中国四川’, ‘中国山东’, ‘中国浙江’, ‘中国山东’, ‘中国台湾’, ‘中国陕西’, ‘中国广东’, ‘中国黑龙江’, ‘中国上海’, ‘中国四川’, ‘中国山东’, ‘中国安徽’, ‘中国安徽’, ‘中国安徽’, ‘中国北京’, ‘中国贵州’, ‘中国吉林’, ‘中国四川’, ‘中国四川’, ‘中国江苏’, ‘中国山东’, ‘中国山东’, ‘中国山东’, ‘中国山东’, ‘中国江苏’, ‘中国四川’, ‘中国山东’, ‘中国山东’, ‘中国广东’, ‘中国浙江’, ‘中国河南’, ‘中国安徽’, ‘中国河南’, ‘中国北京’, ‘中国北京’, ‘马来西亚’, ‘中国湖北’, ‘中国四川’, ‘中国天津’, ‘中国黑龙江’, ‘中国四川’, ‘中国陕西’, ‘中国辽宁’, ‘中国湖南’, ‘中国上海’, ‘中国贵州’, ‘中国山东’, ‘中国湖北’, ‘中国黑龙江’, ‘中国黑龙江’, ‘中国上海’, ‘中国浙江’, ‘中国湖南’, ‘中国台湾’, ‘中国台湾’, ‘中国台湾’, ‘中国台湾’, ‘中国山东’, ‘中国北京’, ‘中国北京’, ‘中国浙江’, ‘中国河南’, ‘中国河南’, ‘中国福建’, ‘中国河南’, ‘中国北京’, ‘中国山东’, ‘中国四川’, ‘中国安徽’, ‘中国河南’, ‘中国四川’, ‘中国湖北’, ‘中国四川’, ‘中国陕西’, ‘中国湖南’, ‘中国四川’, ‘中国台湾’, ‘中国湖北’, ‘中国广西’, ‘中国江西’, ‘中国湖南’, ‘中国湖北’, ‘中国北京’, ‘中国陕西’, ‘中国上海’, ‘中国四川’, ‘中国山东’, ‘中国辽宁’, ‘中国辽宁’, ‘中国台湾’, ‘中国浙江’, ‘中国北京’, ‘中国黑龙江’, ‘中国北京’, ‘中国安徽’, ‘中国河北’, ‘马来西亚’, ‘中国四川’, ‘中国湖南’, ‘中国台湾’, ‘中国广东’, ‘中国上海’, ‘中国四川’, ‘日本’, ‘中国辽宁’, ‘中国黑龙江’, ‘中国浙江’, ‘中国台湾’]
[‘中国湖北’, ‘中国四川’, ‘中国山东’, ‘中国浙江’, ‘中国台湾’, ‘中国陕西’, ‘中国广东’, ‘中国黑龙江’, ‘中国上海’, ‘中国安徽’, ‘中国北京’, ‘中国贵州’, ‘中国吉林’, ‘中国江苏’, ‘中国河南’, ‘马来西亚’, ‘中国天津’, ‘中国辽宁’, ‘中国湖南’, ‘中国福建’, ‘中国广西’, ‘中国江西’, ‘中国河北’, ‘日本’]
[6, 14, 13, 6, 9, 4, 3, 6, 5, 6, 9, 2, 1, 2, 6, 2, 1, 4, 5, 1, 1, 1, 1, 1]在这里插入图片描述

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

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


df = pd.read_json('data/data31557/20200422.json')
#print(df)

grouped=df['name'].groupby(df['zone'])
s = grouped.count()

zone_list = s.index
count_list = s.values


# 设置显示中文
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_result02.jpg')
plt.show()

效果:
Index([‘中国上海’, ‘中国北京’, ‘中国台湾’, ‘中国吉林’, ‘中国四川’, ‘中国天津’, ‘中国安徽’, ‘中国山东’, ‘中国广东’,
‘中国广西’, ‘中国江苏’, ‘中国江西’, ‘中国河北’, ‘中国河南’, ‘中国浙江’, ‘中国湖北’, ‘中国湖南’, ‘中国福建’,
‘中国贵州’, ‘中国辽宁’, ‘中国陕西’, ‘中国黑龙江’, ‘日本’, ‘马来西亚’],
dtype=‘object’, name=‘zone’)
[ 5 9 9 1 14 1 6 13 3 1 2 1 1 6 6 6 5 1 2 4 4 6 1 2]在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np 
import json
import matplotlib.font_manager as font_manager
import pandas as pd
import math


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

#解析json
df = pd.read_json('data/data31557/20200422.json')
grouped=df['name'].groupby(df['weight'])
s = grouped.count()
weight_list = s.index
count_list = s.values

#初始化个数 '<=45kg','45~50kg','50~55kg','>55kg'
count_less_45kg=0
count_less_50kg=0
count_less_55kg=0
count_greater_55kg=0

for index in range(len(weight_list)):
    if weight_list[index]<='45kg':
        count_less_45kg+=count_list[index]
    elif weight_list[index]<='50kg':
        count_less_50kg+=count_list[index]
    elif weight_list[index]<='55kg':
        count_less_55kg+=count_list[index]
    else:
        count_greater_55kg+=count_list[index]

print(count_less_45kg)
print(count_less_50kg)
print(count_less_55kg)
print(count_greater_55kg)

sumz=count_less_45kg+count_less_50kg+count_less_55kg+count_greater_55kg
percent_less_45kg=100*count_less_45kg/sumz
percent_less_50kg=100*count_less_50kg/sumz
percent_less_55kg=100*count_less_55kg/sumz
percent_greater_55kg=100*count_greater_55kg/sumz

labels='<=45kg','45~50kg','50~55kg','>55kg'
sizes=[percent_less_45kg,percent_less_50kg,percent_less_55kg,percent_greater_55kg]
explode=(0,0.1,0,0)
plt.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',shadow=False,startangle=90)
plt.axis('equal')
plt.show()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值