# 同时添加如下代码, 这样每次环境(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')
import matplotlib.pyplot as plt
import numpy as np
import json
import matplotlib.font_manager as font_manager
import os
#显示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()
# 不存在result文件夹就新建一个
if not os.path.exists('work/result'):
os.makedirs('work/result')
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
# 去掉字符串的kg单位并转换成浮点型数字
weights_num = df['weight'].str.rstrip('kg').astype(float)
# 使其切分为不同的[,)半开半闭区间
weights_interval=pd.cut(weights_num,[0,45,50,55,60],right=False)
# 先进行计数再进行排序
weights=weights_interval.value_counts().sort_values(ascending=False)
# 用percent让饼图随着比例而突出 比例越大越突出
weights_percent=weights_interval.value_counts(normalize=True)/4
# 创建特定大小的画布
fig,ax=plt.subplots(figsize=(20,15))
# 引入新的颜色主题
# theme = plt.get_cmap('jet')
# ax.set_prop_cycle("color", [theme(1. * i / len(weights))
# for i in reversed(range(len(weights)))])
# 将weights键值的值提取出来和键拼接字符串格式
weights_count=[str(val) for val in weights.values.tolist()]
# 将键categories标签转换成string并加上单位
weights_labels=weights.index.astype(str)
weights_labels=['<45kg' if label[label.find('[')+1:label.find(',')]=='0'
else label[label.find('[')+1:label.find(',')]+'kg~'+label[label.find(',')+2:label.find(')')]+'kg'
for label in weights_labels]
# 键和值拼接饼图的label
pie_labels=[label+':'+count+'个' for _,(label,count) in enumerate(zip(weights_labels, weights_count))]
fig,l,p=plt.pie(weights, explode=weights_percent,
labels=pie_labels,autopct='%1.1f%%',
shadow=True, startangle=90,textprops={'fontsize': 18})
ax.axis('equal') # 保证比例相等确保画出来的是一个圆而不是椭圆
# 加上图表标注
ax.legend(labels=weights.index)
plt.savefig('/home/aistudio/work/result/pie_result01.jpg')
plt.show()
labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Hogs')
# 先进行计数再进行排序
weights=df['weight'].value_counts().sort_values(ascending=False)
# 用percent让饼图随着比例而突出 比例越大越突出
weights_percent=df['weight'].value_counts(normalize=True)
# 创建特定大小的画布
fig,ax=plt.subplots(figsize=(20,15))
# 由于原来的color颜色会重复 因此需要引入新的颜色主题
theme = plt.get_cmap('hsv')
ax.set_prop_cycle("color", [theme(1. * i / len(weights))
for i in range(len(weights))])
# 将weights键值的值提取出来和键拼接字符串格式
weights_count=[str(val) for val in weights.values.tolist()]
# 由于labels会相互遮挡 因此将其旋转
fig,l,p=plt.pie(weights, explode=weights_percent,labels=weights.index+':'+weights_count+'个',
autopct='%1.1f%%',rotatelabels=True,
shadow=True, startangle=90,textprops={'fontsize': 18})
ax.axis('equal') # 保证比例相等确保画出来的是一个圆而不是椭圆
threshold=0.01 # 旋转比例阈值
#旋转比例过小的切片防止比例文字遮挡
for index,text in enumerate(p):
if weights_percent[index]<threshold:
rot = text.get_rotation()
text.set_rotation(rot-90-(1-rot//180)*180)
ax.legend(labels=weights.index)
plt.savefig('/home/aistudio/work/result/pie_result01.jpg')
plt.show()
plt.get_cmap('hsv')
threshold=0.01 # 旋转比例阈值
#旋转比例过小的切片防止比例文字遮挡
for index,text in enumerate(p):
if weights_percent[index]<threshold:
rot = text.get_rotation()
text.set_rotation(rot-90-(1-rot//180)*180)