!!!作業說明!!!
請在下方提示位置,補充代碼,對《青春有你2》對選手體重分佈進行可視化,繪製餅狀圖,如下圖所示:
不要求跟下圖樣式一模一樣
任務描述:
基於第二天實踐使用Python來爬去百度百科中《青春有你2》所有參賽選手的信息,進行數據可視化分析。
# 如果需要進行持久化安裝, 需要使用持久化路徑, 如下方代碼示例:
#!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
ys
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
--2020-04-24 13:32:43-- https://mydueros.cdn.bcebos.com/font/simhei.ttf
Resolving mydueros.cdn.bcebos.com (mydueros.cdn.bcebos.com)... 182.61.128.198
Connecting to mydueros.cdn.bcebos.com (mydueros.cdn.bcebos.com)|182.61.128.198|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9753388 (9.3M) [application/octet-stream]
Saving to: ‘simhei.ttf.2’
simhei.ttf.2 100%[===================>] 9.30M 54.5MB/s in 0.2s
2020-04-24 13:32:43 (54.5 MB/s) - ‘simhei.ttf.2’ saved [9753388/9753388]
mkdir: cannot create directory ‘.fonts’: File exists
繪製選手區域分佈柱狀圖
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.savefig('./work/result/bar_result.jpg')
plt.show()
No handles with labels found to put in legend.
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]
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-zQkd8Ec6-1587708652295)(output_6_2.png)]
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()
No handles with labels found to put in legend.
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-ZuOvtxwU-1587708652304)(output_7_1.png)]
請在下面完成作業,對選手體重分佈進行可視化,繪製餅狀圖
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.pie(x=count_list ,labels=zone_list,autopct='%1.1f%%',shadow=False,startangle=150)
plt.legend()
plt.title('''《青春有你2》參賽選手''',fontsize = 24)
plt.savefig('/home/aistudio/work/result/pie_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]
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-U6kROP8t-1587708652309)(output_9_1.png)]