一、准备工作
二、可视化步骤
2.1 导入相关库
import datetime
import pandas as pd
import akshare as ak
from pyecharts import options as opts
from pyecharts.charts import Map
2.2 高风险地区数据获取
covid_19_risk_area_df = ak.covid_19_risk_area(symbol="高风险等级地区")
2.3 分省高风险地区数量统计、排序
df_counts_province = covid_19_risk_area_df.groupby(['grade','province']).nunique()[['communitys']]
df_counts_province = df_counts_province.sort_values(by=['communitys'],ascending=False)
df_counts_province.rename(columns={"communitys":"counts"},inplace=True)
2.4 获取省份简称
ProvinceInfo_TX = pd.read_excel("ProvinceInfo_TX.xlsx")
df = pd.merge(df_counts_province.reset_index(),ProvinceInfo_TX,how="left",left_on="province",right_on="fullname")
2.5 pyecharts 绘图
data_list = df[['name','counts']].values.tolist()
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
subtitle = "数据来源:AKShare->事件数据->COVID->疫情风险等级查询\n更新日期:{}".format(now)
c = (
Map()
.add("高风险地区数量", data_list, "china")
.set_global_opts(
title_opts=opts.TitleOpts(
title="全国内地高风险地区数量分布",
subtitle=subtitle,
),
legend_opts=opts.LegendOpts(
selected_mode="single",
),
visualmap_opts=opts.VisualMapOpts(
is_piecewise=True,
pieces = [
{
"max":20,
"label":"1-20",
"color":"#FFFF00",
},{
"min":20,
"max":40,
"label":"20-40",
"color":"#FFE500",
},{
"min":40,
"max":80,
"label":"40-80",
"color":"#FFCC00",
},{
"min":80,
"max":150,
"label":"80-150",
"color":"#FF9900",
},{
"min":150,
"max":300,
"label":"150-300",
"color":"#FF6600",
},{
"min":300,
"label":"300及以上",
"color":"#FF3300",
},
]
),
)
)
c.render_notebook()
三、完整代码
import datetime
import pandas as pd
import akshare as ak
from pyecharts import options as opts
from pyecharts.charts import Map
covid_19_risk_area_df = ak.covid_19_risk_area(symbol="高风险等级地区")
df_counts_province = covid_19_risk_area_df.groupby(['grade','province']).nunique()[['communitys']]
df_counts_province = df_counts_province.sort_values(by=['communitys'],ascending=False)
df_counts_province.rename(columns={"communitys":"counts"},inplace=True)
df_counts_city = covid_19_risk_area_df.groupby(['grade','province','city']).nunique()[['communitys']]
df_counts_city = df_counts_city.sort_values(by=['communitys'],ascending=False)
df_counts_city.rename(columns={"communitys":"counts"},inplace=True)
ProvinceInfo_TX = pd.read_excel("ProvinceInfo_TX.xlsx")
df = pd.merge(df_counts_province.reset_index(),ProvinceInfo_TX,how="left",left_on="province",right_on="fullname")
data_list = df[['name','counts']].values.tolist()
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
subtitle = "数据来源:AKShare->事件数据->COVID->疫情风险等级查询\n更新日期:{}".format(now)
c = (
Map()
.add("高风险地区数量", data_list, "china")
.set_global_opts(
title_opts=opts.TitleOpts(
title="全国内地高风险地区数量分布",
subtitle=subtitle,
),
legend_opts=opts.LegendOpts(
selected_mode="single",
),
visualmap_opts=opts.VisualMapOpts(
is_piecewise=True,
pieces = [
{
"max":20,
"label":"1-20",
"color":"#FFFF00",
},{
"min":20,
"max":40,
"label":"20-40",
"color":"#FFE500",
},{
"min":40,
"max":80,
"label":"40-80",
"color":"#FFCC00",
},{
"min":80,
"max":150,
"label":"80-150",
"color":"#FF9900",
},{
"min":150,
"max":300,
"label":"150-300",
"color":"#FF6600",
},{
"min":300,
"label":"300及以上",
"color":"#FF3300",
},
]
),
)
)
c.render_notebook()
. . .
|