前序
你是否看到网上各种优美可视化图表,心想到底是如何通过绘画出来的,今天我们用pyecharts来实现它。
数据可视化
这里,我们以疫情数据作为参考指标,数据通过接口获取。
url = "https://lab.isaaclin.cn/nCoV/api/area"
data = requests.get(url).json()
1. 全球疫情累计死亡人数分布图:
def yiqing_world(data):
world_data = []
for item in data["results"]:
if item["countryEnglishName"]:
world_data.append([item["countryEnglishName"]
.replace('United States of America', 'United States')
.replace('United Kingdom', 'Greenland'),
item["deadCount"]])
_max = max([item[1] for item in world_data])
world_map = (
Map(init_opts=opts.InitOpts(theme='romantic'))
.add("累计死亡人数", world_data, 'world', is_map_symbol_show=False)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="全球疫情累计死亡人数分布图", pos_left="left"),
legend_opts=opts.LegendOpts(is_show=False),
visualmap_opts=opts.VisualMapOpts(max_=_max, is_piecewise=True)
)
)
world_map.render("全球疫情累计死亡人数分布图.html")
image
2. 中国疫情确诊人数分布地图:
def yiqing_china(data):
china_data = []
for item in data["results"]:
if item["countryName"] == "中国":
china_data.append([item["provinceShortName"], item["confirmedCount"]])
_max = max([item[1] for item in china_data])
china_map = (
Map(init_opts=opts.InitOpts(theme='dark'))
.add("确诊人数", china_data, "china", is_map_symbol_show=False)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="中国疫情确诊人数分布地图"),
legend_opts=opts.LegendOpts(is_show=True),
visualmap_opts=opts.VisualMapOpts(max_=_max, is_piecewise=True)
)
)
china_map.render("中国疫情确诊人数分布地图.html")
image
3. 广东疫情确诊人数分布地图
def yiqing_guangdong(data):
guangdong_data = []
for item in data["results"]:
if item["provinceShortName"] == "广东":
guangdong_data = [[i["cityName"]+"市", i["confirmedCount"]]for i in item["cities"]]
_max = max([item[1] for item in guangdong_data])
china_map = (
Map(init_opts=opts.InitOpts(theme='dark'))
.add("确诊人数", guangdong_data, "广东", is_map_symbol_show=False)
# is_show=False 不展示市分布
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="广东疫情确诊人数分布地图"),
legend_opts=opts.LegendOpts(is_show=True),
visualmap_opts=opts.VisualMapOpts(max_=_max, is_piecewise=True)
)
)
china_map.render("广东疫情确诊人数分布地图.html")
image
4. 海南新冠疫情确认治愈情况
def yi_hainan(data):
hn_data = []
for item in data["results"]:
if item["provinceShortName"] == "海南":
hn_data = item["cities"]
hn_bar = (
Bar(init_opts=opts.InitOpts(theme='dark'))
.add_xaxis([hn["cityName"] for hn in hn_data])
.add_yaxis("累计确诊人数", [hn["confirmedCount"] for hn in hn_data])
.add_yaxis("累计治愈人数", [hn["curedCount"] for hn in hn_data])
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="海南新冠疫情确认治愈情况"))
)
hn_bar.render("海南新冠疫情确认治愈情况.html")
image
5. 中国疫情热力分布图
def yiqing_china_hot(data):
cities_data = []
for item in data["results"]:
if item["countryName"] == "中国":
if item['cities']:
cities_data.extend(item['cities'])
# pyecharts.datasets.COORDINATES.keys() 城市坐标
line = ((i["cityName"], i["currentConfirmedCount"]) for i in cities_data if i['cityName'] in pyecharts.datasets.COORDINATES.keys())
hot_geo = (
Geo(init_opts=opts.InitOpts(theme='dark'))
.add_schema(maptype="china")
.add("中国疫情热力分布图", line, type_=ChartType.HEATMAP)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="中国疫情热力分布图"),
legend_opts=opts.LegendOpts(is_show=False),
visualmap_opts=opts.VisualMapOpts(is_show=True))
)
hot_geo.render("中国疫情热力分布图.html")
image
6.广东疫情确诊人数分布云图
def yiqing_cloud(data):
guangdong_data = []
for item in data["results"]:
if item["provinceShortName"] == "广东":
guangdong_data = [[i["cityName"]+"市", i["confirmedCount"]]for i in item["cities"]]
china_map = (
WordCloud(init_opts=opts.InitOpts(theme='essos'))
.add("累计死亡人数", guangdong_data, word_size_range=[20, 100],
textstyle_opts=opts.TextStyleOpts(font_family="cursive"))
.set_global_opts(title_opts=opts.TitleOpts(title="广东疫情确诊人数分布云图"))
)
china_map.render("广东疫情确诊人数分布云图.html")
image
image
更多源码,请查看原文