赞
踩
数据有每个国家对应的近20年的对于儿童超重和营养不良占比和人数的三种类型(estimate字段)评估数据,一共四个sheet,就是儿童超重比例、儿童超重人数、儿童营养不良比例、儿童营养不良人数。
要求:儿童超重情况可视化分析:对各国儿童超重的比例和人数进行划分,根据数值的大小区分超重的程度,绘制图表,统计并对比 2000 年与 2020 年不同程度分布情况;
思路:先提取出表中每个国家超重比例和超重人数,然后再提取对应的2000年与2020年数据,表中数据要从字符串型转为浮点型数据才能画图。查看每年数据的数值分布,确定轻度超重比例小于5.0,重度超重比例大于11.0,中度居中;轻度超重人数小于27万人,重度超重人数大于257万人,中度居中,画出四个饼图
要求:绘制地理图,分析不同国家儿童营养不良的情况;
解决思路:先提取出每个国家重度营养不良的数据,对其所有年份数据求和,根据和还有国家名称绘制世界地图,世界地图生成html文件在同一目录下,打开文件即可在浏览器观察世界地图
要求:根据不同程度,抽取排名前 3 的国家数据,整合数据分析儿童营养不良不同程度数据的差距情况;
解决思路:
根据第二部取出营养不良总和前三的国家,去原表中找到对应的数据,根据数据绘制三个国家轻度营养不良、中度营养不良、重度营养不良的折线图对比
要求:分析对比 2000 年与 2020 年各国儿童超重的比例和人数变动情况
解决思路:根据第一部处理的数据,提取前30个国家的数据绘制2000年与2020年的超重比例和人数柱状图
存一下源码,方便以后用到回来看看
- import pandas as pd
- data_proportation = pd.read_excel('Country_Estimates_2021.xlsx',sheet_name = 'Overweight Proportion (Model)')
- data_numbers = pd.read_excel('Country_Estimates_2021.xlsx',sheet_name = 'Overweight Numb Affected(Model)')
-
- data_proportation_over = data_proportation[data_proportation['Estimate'] == 'Upper Uncertainty Bound']
- data_numbers_over = data_numbers[data_numbers['Estimate'] =='Upper Uncertainty Bound']
- data_proportation_over['2000'] = pd.to_numeric(data_proportation_over['2000'],errors='coerce')
- data_proportation_over['2020 1'] = pd.to_numeric(data_proportation_over['2020 1'],errors='coerce')
- data_numbers_over['2000'] = pd.to_numeric(data_numbers_over['2000'],errors='coerce')
- data_numbers_over['2020 1'] = pd.to_numeric(data_numbers_over['2020 1'],errors='coerce')
-
- from matplotlib import pyplot as plt
- from jupyterthemes import jtplot
- jtplot.style(theme='chesterish') #选择一个绘图主题
- plt.rcParams['font.sans-serif'] = 'SimHei' # 设置中文显示
- plt.rcParams['axes.unicode_minus'] = False
- print(data_proportation_over['2000'].describe())
- data_proportation_over_2000 = []
- data_proportation_over_2000.append(data_proportation_over['2000'][data_proportation_over['2000']<=5.0].count())
- data_proportation_over_2000.append(data_proportation_over['2000'][(data_proportation_over['2000']>=5.0) & (data_proportation_over['2000']<=11.0)].count())
- data_proportation_over_2000.append(data_proportation_over['2000'][data_proportation_over['2000']>=11.0].count())
- label = ['轻度超重', '中度超重', '重度超重'] # 刻度标签
- plt.figure(figsize=(6, 6)) # 将画布设定为正方形,则绘制的饼图是正圆
- explode = [0.01, 0.01, 0.01] # 设定各项离心n个半径
- plt.pie(data_proportation_over_2000,explode=explode, labels=label, autopct='%1.1f%%') # 绘制饼图
- plt.title('2000年世界超重儿童比例饼图') # 添加图表标题
- # plt.savefig('../tmp/2019年各年龄段年末总人口饼图.png')
- plt.show()
-
- print(data_proportation_over['2020 1'].describe())
- data_proportation_over_2020 = []
- data_proportation_over_2020.append(data_proportation_over['2020 1'][data_proportation_over['2020 1']<=5.0].count())
- data_proportation_over_2020.append(data_proportation_over['2020 1'][(data_proportation_over['2020 1']>=5.0) & (data_proportation_over['2020 1']<=11.0)].count())
- data_proportation_over_2020.append(data_proportation_over['2020 1'][data_proportation_over['2020 1']>=11.0].count())
- label = ['轻度超重', '中度超重', '重度超重'] # 刻度标签
- plt.figure(figsize=(6, 6)) # 将画布设定为正方形,则绘制的饼图是正圆
- explode = [0.01, 0.01, 0.01] # 设定各项离心n个半径
- plt.pie(data_proportation_over_2020,explode=explode, labels=label, autopct='%1.1f%%') # 绘制饼图
- plt.title('2000年世界超重儿童比例饼图') # 添加图表标题
- # plt.savefig('../tmp/2019年各年龄段年末总人口饼图.png')
- plt.show()
-
- print(data_numbers_over['2000'].describe())
- data_numbers_over_2000 = []
- data_numbers_over_2000.append(data_numbers_over['2000'][data_numbers_over['2000']<=27.0].count())
- data_numbers_over_2000.append(data_numbers_over['2000'][(data_numbers_over['2000']>=27.0) & (data_numbers_over['2000']<=257.0)].count())
- data_numbers_over_2000.append(data_numbers_over['2000'][data_numbers_over['2000']>=257].count())
- label = ['轻度超重', '中度超重', '重度超重'] # 刻度标签
- plt.figure(figsize=(6, 6)) # 将画布设定为正方形,则绘制的饼图是正圆
- explode = [0.01, 0.01, 0.01] # 设定各项离心n个半径
- plt.pie(data_numbers_over_2000,explode=explode, labels=label, autopct='%1.1f%%') # 绘制饼图
- plt.title('2020年世界超重儿童比例饼图') # 添加图表标题
- # plt.savefig('../tmp/2019年各年龄段年末总人口饼图.png')
- plt.show()
-
- print(data_numbers_over['2020 1'].describe())
- data_numbers_over_2020 = []
- data_numbers_over_2020.append(data_numbers_over['2020 1'][data_numbers_over['2020 1']<=27.0].count())
- data_numbers_over_2020.append(data_numbers_over['2020 1'][(data_numbers_over['2020 1']>=27.0) & (data_numbers_over['2020 1']<=257.0)].count())
- data_numbers_over_2020.append(data_numbers_over['2020 1'][data_numbers_over['2020 1']>=257].count())
- label = ['轻度超重', '中度超重', '重度超重'] # 刻度标签
- plt.figure(figsize=(6, 6)) # 将画布设定为正方形,则绘制的饼图是正圆
- explode = [0.01, 0.01, 0.01] # 设定各项离心n个半径
- plt.pie(data_numbers_over_2020,explode=explode, labels=label, autopct='%1.1f%%') # 绘制饼图
- plt.title('2020年世界超重儿童比例饼图') # 添加图表标题
- # plt.savefig('../tmp/2019年各年龄段年末总人口饼图.png')
- plt.show()
-
- data_proportation_Stunting = pd.read_excel('Country_Estimates_2021.xlsx',sheet_name = 'Stunting Proportion (Model)')
- #提取各国严重营养不良的数据
- data_proportation_Stunting_over_e = pd.DataFrame()
- data_proportation_Stunting_over = data_proportation_Stunting[data_proportation_Stunting['Estimate'] == 'Upper Uncertainty Bound']
- for i in ['2000','2001','2002','2003','2004','2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016','2017','2018','2019','2020 1']:
- data_proportation_Stunting_over_e[i] = pd.to_numeric(data_proportation_Stunting_over[i],errors='coerce')
- data_proportation_Stunting_over_e['行的和'] = data_proportation_Stunting_over_e.sum(axis=1)
- print(data_proportation_Stunting_over_e['行的和'])
- data_proportation_Stunting_over_e['country'] = data_proportation_Stunting_over['Country and areas']
- print(data_proportation_Stunting_over_e.head())
-
- list_data = []
- for i in data_proportation_Stunting_over_e.itertuples():
- list_data.append([getattr(i, 'country'),getattr(i, '行的和')])
- list_data
-
- from pyecharts import options as opts
- from pyecharts.charts import Map
- import rando
- def create_world_map(data):
- '''
- 作用:生成世界地图
- '''
- ( # 大小设置
- Map()
- .add(
- series_name="严重营养不良",
- data_pair=data,
- maptype="world",
- )
- # 全局配置项
- .set_global_opts(
- # 设置标题
- title_opts=opts.TitleOpts(title="世界地图"),
- # 设置标准显示
- visualmap_opts=opts.VisualMapOpts(max_=1000, is_piecewise=False),
- )
- # 系列配置项
- .set_series_opts(
- # 标签名称显示,默认为True
- label_opts=opts.LabelOpts(is_show=False, color="blue")
- )
- # 生成本地html文件
- .render("世界地图.html")
- )
- create_world_map(list_data)
-
- data_top_3 = data_proportation_Stunting_over_e.sort_values(by="行的和")
- data_top_3 = data_top_3.tail(3)
- print(data_top_3)
- list_top_3 = list(data_top_3['country'])
- print(list_top_3)
-
- data_top_3 =data_proportation_Stunting[(data_proportation_Stunting['Country and areas'] == 'Burundi')|(data_proportation_Stunting['Country and areas'] == 'Eritrea')|(data_proportation_Stunting['Country and areas'] == 'Timor-Leste')]
- data_top_3_point = data_top_3[data_top_3['Estimate'] =='Point Estimate']
- data_top_3_lower = data_top_3[data_top_3['Estimate'] =='Lower Uncertainty Bound']
- data_top_3_upper = data_top_3[data_top_3['Estimate'] =='Upper Uncertainty Bound']
- data_top_3_upper
- list_columns =['2000','2001','2002','2003','2004','2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016','2017','2018','2019','2020 1']
-
- plt.figure(figsize=(15, 10)) # 设置画布
- plt.plot(list_columns, data_top_3_point.iloc[0][list_columns], marker='o', c='red') # 绘制散点图
- plt.plot(list_columns, data_top_3_point.iloc[1][list_columns], marker='D', c='blue') # 绘制散点图
- plt.plot(list_columns, data_top_3_point.iloc[2][list_columns], marker='v', c='yellow') # 绘制散点图'''
- plt.xlabel('年份') # 添加横轴标签
- plt.ylabel('轻度营养不良') # 添加纵轴标签
- plt.title('2000-2020年各前三国家轻度营养不良散点图') # 添加图表标题
- plt.legend(['Burundi', 'Eritrea', 'Timor-Leste']) #添加图例
- #plt.savefig('../tmp/2000-2019年各年龄段年末总人口散点图.png')
- plt.show()
-
- plt.figure(figsize=(15, 10)) # 设置画布
- plt.plot(list_columns, data_top_3_lower.iloc[0][list_columns], marker='o', c='red') # 绘制散点图
- plt.plot(list_columns, data_top_3_lower.iloc[1][list_columns], marker='D', c='orange') # 绘制散点图
- plt.plot(list_columns, data_top_3_lower.iloc[2][list_columns], marker='v', c='yellow') # 绘制散点图'''
- plt.xlabel('年份') # 添加横轴标签
- plt.ylabel('轻度营养不良') # 添加纵轴标签
- plt.title('2000-2020年各前三国家重度营养不良散点图') # 添加图表标题
- plt.legend(['Burundi', 'Eritrea', 'Timor-Leste']) #添加图例
- #plt.savefig('../tmp/2000-2019年各年龄段年末总人口散点图.png')
- plt.show()
-
- plt.figure(figsize=(15, 10)) # 设置画布
- plt.plot(list_columns, data_top_3_upper.iloc[0][list_columns], marker='o', c='red',linestyle='--') # 绘制散点图
- plt.plot(list_columns, data_top_3_upper.iloc[1][list_columns], marker='D', c='green') # 绘制散点图
- plt.plot(list_columns, data_top_3_upper.iloc[2][list_columns], marker='v', c='yellow') # 绘制散点图'''
- plt.xlabel('年份') # 添加横轴标签
- plt.ylabel('轻度营养不良') # 添加纵轴标签
- plt.title('2000-2020年各前三国家中度营养不良散点图') # 添加图表标题
- plt.legend(['Burundi', 'Eritrea', 'Timor-Leste']) #添加图例
- #plt.savefig('../tmp/2000-2019年各年龄段年末总人口散点图.png')
- plt.show()
-
- data_proportation_over = data_proportation_over.head(20)
- data_numbers_over = data_numbers_over.head(20)
- import numpy as np
- length = len(data_proportation_over['2000'])
- x = np.arange(length)
- plt.figure()
- total_width, n = 0.8, 2 # 柱状图总宽度,有几组数据
- width = total_width / n # 单个柱状图的宽度
- x1 = x - width / 2 # 第一组数据柱状图横坐标起始位置
- x2 = x1 + width # 第二组数据柱状图横坐标起始位置
- plt.figure(figsize=(30, 10))
- plt.title("不同国家2000年与2020年超重比例指标对比") # 柱状图标题
- plt.ylabel("超重比例") # 纵坐标label
- plt.bar(x1, data_proportation_over['2000'], width=width, label="2000年")
- plt.bar(x2, data_proportation_over['2020 1'], width=width, label="2020年")
- plt.xticks(x, data_proportation_over['Country and areas']) # 用星期几替换横坐标x的值
- plt.legend() # 给出图例
- plt.show()
-
- length = len(data_numbers_over['2000'])
- x = np.arange(length)
- plt.figure()
- total_width, n = 0.8, 2 # 柱状图总宽度,有几组数据
- width = total_width / n # 单个柱状图的宽度
- x1 = x - width / 2 # 第一组数据柱状图横坐标起始位置
- x2 = x1 + width # 第二组数据柱状图横坐标起始位置
- plt.figure(figsize=(30, 10))
- plt.title("不同国家2000年与2020年超重人数对比") # 柱状图标题
- plt.ylabel("超重人数") # 纵坐标label
- plt.bar(x1, data_numbers_over['2000'], width=width,color ='yellow' ,label="2000年")
- plt.bar(x2, data_numbers_over['2020 1'], width=width,color='red' , label="2020年")
- plt.xticks(x, data_numbers_over['Country and areas']) # 用星期几替换横坐标x的值
- plt.legend() # 给出图例
- plt.show()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。