大家好,我是EverdayForCode。你,今天学习了吗?
对《青春有你2》对选手体重分布进行可视化,绘制饼状图,如下图所示(不要求一样):
直接上代码:
!mkdir /home/aistudio/work/result
# 如果需要进行持久化安装, 需要使用持久化路径, 如下方代码示例:
#!mkdir /home/aistudio/external-libraries
#!pip install matplotlib -t /home/aistudio/external-libraries
# 同时添加如下代码, 这样每次环境(kernel)启动的时候只要运行下方代码即可:
# Also add the following code, so that every time the environment (kernel) starts, just run the following code:
import sys
sys.path.append('/home/aistudio/external-libraries')
# 下载中文字体
!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')
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()
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轴为该区域的小姐姐数量
weights = []
for star in json_array:
weight = float(star['weight'].replace('kg',''))
weights.append(weight)
print(len(weights))
print(weights)
size1,size2,size3,size4 = 0,0,0,0
for weight in weights:
if weight <= 45:
size1 += 1
elif 45 < weight <= 50:
size2 += 1
elif 50 < weight <= 55:
size3 += 1
else:
size4 += 1
labels = ['<=45kg','45~50kg','50~55kg','>55kg']
sizes = [size1,size2,size3,size4]
explode = (0,0,0,0)
colors = ['mediumspringgreen','lightcoral','forestgreen','slateblue']
fig1,ax1 = plt.subplots()
ax1.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',
shadow=True,startangle=90,colors=colors)
ax1.axis('equal')
plt.legend(loc='upper left')
plt.savefig('/home/aistudio/work/result/weight_01.jpg')
plt.show()
# 方法二使用pandas
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)
weights = df['weight']
arrs = weights.values
print(arrs)
# 去掉kg并转换为float
for i in range(len(arrs)):
arrs[i] = float(arrs[i][0:-2])
print(arrs)
# pandas.cut把一组数据分割成离散区间
bin = [0,45,50,55,100]
labels = ['<=45kg','45~50kg','50~55kg','>55kg']
se1 = pd.cut(arrs,bin,labels=labels)
# 对se1每个值计数并排序
pd.value_counts(se1)
sizes = pd.value_counts(se1)
labels = sizes.index
explode = (0.1,0.1,0.1,0.1)
colors = ['mediumspringgreen','lightcoral','forestgreen','slateblue']
fig1,ax1 = plt.subplots()
ax1.pie(sizes,explode=explode,labels=labels,autopct='%1.1f%%',
shadow=True,startangle=90,colors=colors)
ax1.axis('equal')
plt.savefig('/home/aistudio/work/result/weight_02.jpg')
plt.show()
这篇博客没多写逻辑处理,都在代码里。