绘制简易地图
导包
import json
from pyecharts.charts import Map
from pyecharts.options import *
准备地图对象
map = Map()
准备数据
data = [
("新疆维吾尔自治区", 99),
("黑龙江省", 199),
("江苏省", 299),
("辽宁省", 399),
("云南省", 499)
]
添加数据
map.add("测试地图", data, "china")
全局选项
map.set_global_opts(
visualmap_opts=VisualMapOpts(
is_show=True,
is_piecewise=True,
pieces=[
{"min": 1, "max": 99, "label": "1-99", "color": ""},
{"min": 100, "max": 199, "label": "100-199", "color": ""},
{"min": 200, "max": 299, "label": "200-299", "color": ""},
{"min": 300, "max": 399, "label": "300-399", "color": ""},
{"min": 400, "max": 499, "label": "400-499", "color": ""}
]
)
)
绘图
map.render()
案例1,全国疫情地图
读取数据文件
f = open("D:/疫情.txt", "r", encoding="utf-8")
data = f.read()
f.close()
print(type(data))
将json转为字典
data_dict = json.loads(data)
print(type(data_dict))
取出各省数据
province_data_list = data_dict["areaTree"][0]["children"]
print(type(province_data_list))
data_list = []
for province_data in province_data_list:
province_name = province_data["name"]
province_num = province_data["total"]["confirm"]
data_list.append((province_name, province_num))
print(data_list)
绘图
map = Map()
map.add("疫情地图", data_list, "china")
map.set_global_opts(
title_opts=TitleOpts(title="全国疫情地图", pos_bottom="1%", pos_left="center"),
legend_opts=LegendOpts(is_show=True),
toolbox_opts=ToolboxOpts(is_show=True),
visualmap_opts=VisualMapOpts(
is_show=True,
is_piecewise=True,
pieces=[
{"min": 1, "max": 199, "label": "1-199", "color": ""},
{"min": 200, "max": 399, "label": "200-399", "color": ""},
{"min": 400, "max": 599, "label": "400-599", "color": ""},
{"min": 600, "max": 799, "label": "600-799", "color": ""},
{"min": 800, "max": 999, "label": "800-999", "color": ""},
{"min": 1000, "max": 2999, "label": "1000-2999", "color": ""},
{"min": 3000, "max": 79999, "label": "3000-79999", "color": ""}
]
)
)
map.render("疫情地图.html")
案例二,取出某省份数据,绘制疫情地图
import json
from pyecharts.charts import Map
from pyecharts.options import *
f = open("D:/疫情.txt", "r", encoding="utf-8")
data = f.read()
f.close()
data_dict = json.loads(data)
jiangsu_data = data_dict['areaTree'][0]['children'][1]
data_list = jiangsu_data['children']
list_city = []
for element in data_list:
name = element['name']
num = element['total']['confirm']
list_city.append((name, num))
map = Map()
map.add("江苏省疫情地图", list_city, "江苏")
map.set_global_opts(
title_opts=TitleOpts(title="江苏各市疫情地图", pos_bottom="1%", pos_left="center"),
legend_opts=LegendOpts(is_show=True),
toolbox_opts=ToolboxOpts(is_show=True),
visualmap_opts=VisualMapOpts(
is_show=True,
is_piecewise=True,
pieces=[
{"min": 1, "max": 199, "label": "1-199", "color": ""},
{"min": 200, "max": 299, "label": "200-299", "color": ""},
{"min": 300, "max": 399, "label": "300-399", "color": ""},
{"min": 400, "max": 499, "label": "400-499", "color": ""},
{"min": 500, "max": 599, "label": "500-599", "color": ""},
{"min": 600, "max": 699, "label": "600-699", "color": ""},
{"min": 700, "max": 799, "label": "700-799", "color": ""}
]
)
)
map.render("江苏.html")