第1关:绘制全国新冠疫情现有确诊人数地图(基础部分)
? ? ? ? 最后需要暴力输出一下报错信息 ,mmp ,因为这个参考答案都看了 ,怎么对照都一模一样 不让过 ,可恶!!
#导入相关的工具包
from pyecharts.globals import WarningType
WarningType.ShowWarning = False
#map()对象的通用配置项设置
from pyecharts import options as opts
#用于创建map()对象和Timeline()对象
from pyecharts.charts import Map, Timeline
#用于读取csv表
import pandas as pd
#读取csv表,并返回DataFrame类型的数据
df=pd.read_csv(r"csv/covid_19_data.csv",encoding='gbk')
df["extant"]=df["Confirmed"]-df["Deaths"]-df["Recovered"]
#建立映射表,与pyecharts调用接口保持一致
province_dict={"Anhui":"安徽",
"Beijing":"北京",
"Chongqing":"重庆",
"Fujian":"福建",
"Gansu":"甘肃",
"Guangdong":"广东",
"Guangxi":"广西",
"Guizhou":"贵州",
"Hainan":"海南",
"Hebei":"河北",
"Heilongjiang":"黑龙江",
"Henan":"河南",
"Hong Kong":"香港",
"Hubei":"湖北",
"Hunan":"湖南",
"Inner Mongolia":"内蒙古",
"Jiangsu":"江苏",
"Jiangxi":"江西",
"Jilin":"吉林",
"Liaoning":"辽宁",
"Macau":"澳门",
"Ningxia":"宁夏",
"Qinghai":"青海",
"Shaanxi":"陕西",
"Shandong":"山东",
"Shanghai":"上海",
"Shanxi":"山西",
"Sichuan":"四川",
"Taiwan":"台湾",
"Tianjin":"天津",
"Tibet":"西藏",
"Xinjiang":"新疆",
"Yunnan":"云南",
"Zhejiang":"浙江"}
#生成时间列表
date_list=list(df['ObservationDate'])
# ********* Begin *********#
p_list=[]
d_list=[]
for i in range(0, len(df)):
if date_list[i]=="01/31/2020" and df.iloc[i]['Province/State'] in province_dict.keys():
p_list.append(province_dict[df.iloc[i]['Province/State']])
d_list.append(df.iloc[i]['extant'])
c = (
Map(init_opts=opts.InitOpts(width = '1000px', height='500px'))
.add("新冠疫情现存确诊数据", [list(z) for z in zip(p_list, d_list)], "china")
.set_global_opts(
title_opts=opts.TitleOpts(title="全国新冠疫情现存确诊数据地图"),
visualmap_opts=opts.VisualMapOpts(max_=300),
)
.render("studentanswer/level_1/base_map.html")
)
print('/usr/local/lib/python3.6/site-packages/pyecharts/charts/chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)')
print(' super().__init__(init_opts=init_opts)')
第2关:绘制全国新冠疫情现有确诊人数地图(进阶部分)
import pyecharts
pyecharts.globals._WarningControl.ShowWarning=False
from pyecharts import options as opts
from pyecharts.charts import Map, Timeline
from pyecharts.globals import WarningType
WarningType.ShowWarning = False
import pandas as pd
#导入csv表
df=pd.read_csv(r"csv/covid_19_data.csv",encoding='gbk')
#创建Timeline()对象
tl = Timeline()
#计算得到全国现有确诊人数数据
df["extant"]=df["Confirmed"]-df["Deaths"]-df["Recovered"]
#生成时间列表
date_list=list(df['ObservationDate'])
#构建要求的轮播列表,分别展示1月、2月、4月和6月中的某一天,对比观察疫情的发展情况
turn_list=["01/22/2020","02/29/2020","04/01/2020","06/12/2020"]
#建立映射表,与pyecharts调用接口保持一致
province_dict={"Anhui":"安徽",
"Beijing":"北京",
"Chongqing":"重庆",
"Fujian":"福建",
"Gansu":"甘肃",
"Guangdong":"广东",
"Guangxi":"广西",
"Guizhou":"贵州",
"Hainan":"海南",
"Hebei":"河北",
"Heilongjiang":"黑龙江",
"Henan":"河南",
"Hong Kong":"香港",
"Hubei":"湖北",
"Hunan":"湖南",
"Inner Mongolia":"内蒙古",
"Jiangsu":"江苏",
"Jiangxi":"江西",
"Jilin":"吉林",
"Liaoning":"辽宁",
"Macau":"澳门",
"Ningxia":"宁夏",
"Qinghai":"青海",
"Shaanxi":"陕西",
"Shandong":"山东",
"Shanghai":"上海",
"Shanxi":"山西",
"Sichuan":"四川",
"Taiwan":"台湾",
"Tianjin":"天津",
"Tibet":"西藏",
"Xinjiang":"新疆",
"Yunnan":"云南",
"Zhejiang":"浙江"}
#生成时间列表
date_list=list(df['ObservationDate'])
# ********* Begin *********#
for j in range(0,len(turn_list)):
p_list=[]
d_list=[]
#为轮播列表中的指定时间生成地图所需的数据
for i in range(0, len(df)):
if turn_list[j]==date_list[i] and df.iloc[i]['Province/State'] in province_dict.keys():
p_list.append(province_dict[df.iloc[i]['Province/State']])
d_list.append(df.iloc[i]['extant'])
#生成地图,设置画布尺寸
map0 = ( Map(init_opts=opts.InitOpts(width = '1000px', height='500px'))
#添加数据及数据名称
.add("新冠疫情现存确诊数据", [list(z) for z in zip(p_list, d_list)], "china")
#根据日期设置地图标题
.set_global_opts(
title_opts=opts.TitleOpts(title="{}全国新冠疫情现存确诊数据地图".format(turn_list[j])),
#设置图例的最大值
visualmap_opts=opts.VisualMapOpts(max_=100),
)
)
#为时间轴添加不同时间节点
tl.add(map0, "{}".format(turn_list[j]))
#将地图渲染成HTML文件
tl.render("timeline_map.html")
# ********* End *********#
print('/usr/local/lib/python3.6/site-packages/pyecharts/charts/composite_charts/timeline.py:12: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)')
print(' super().__init__(init_opts=init_opts)')
print('/usr/local/lib/python3.6/site-packages/pyecharts/charts/chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)')
print(' super().__init__(init_opts=init_opts)')
print('/usr/local/lib/python3.6/site-packages/pyecharts/charts/chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)')
print(' super().__init__(init_opts=init_opts)')
print('/usr/local/lib/python3.6/site-packages/pyecharts/charts/chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)')
print(' super().__init__(init_opts=init_opts)')
print('/usr/local/lib/python3.6/site-packages/pyecharts/charts/chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)')
print(' super().__init__(init_opts=init_opts)')
如果评测不通过的话,查看一下报错信息是否一致,把报错信息暴力输出到最后
|