数据可视化-地图可视化

更新时间: 2024-03-26 15:39:41

# 地图演示

"""
演示地图可视化的基本使用
"""
from pyecharts.charts import Map

# 准备地图对象
map = Map()

# 准备数据
data = [
    ("北京市",99),
    ("上海市", 199),
    ("湖南省", 299),
    ("台湾省", 399),
    ("广东省", 499),
]


# 添加数据
map.add("测试地图",data,"china")

# 绘图
map.render()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# 基础地图演示 - 视觉映射器

map.set_global_opts(
    visualmap_opts = VisualMapOpts(
        is_show=True,
        is_piecewise=True,
        pieces=[
            {"min":1, "max":9, "label":"1-9人", "color": "#CCFFFF"},
            {"min":10, "max":99, "label":"10-99人", "color": "#FFFF99"},
            {"min":100, "max":499, "label":"100-499人", "color": "#FF9966"},
            {"min":500, "max":999, "label":"500-999人", "color": "#FF6666"},
            {"min":1000, "max":9999, "label":"1000-9999人", "color": "#CC3333"},
            {"min":10000, "label":"10000以上", "color": "#990033"}
        ]
    )
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 国内疫情地图构建

"""
全国疫情可视化地图开发
"""
import json
from pyecharts.charts import Map
from pyecharts.options import *

# 读取数据文件
f = open("C:/Users/liuwei/Desktop/疫情.txt", encoding="UTF-8")
data = f.read()
# 关闭文件
f.close()
# 取到各省数据
# 将字符串json转换为python的字典
data_dict = json.loads(data)
# 从字典中取出省份数据
province_data_list = data_dict["areaTree"][0]["children"]
# 组装每个省份和确诊人数为元组,并各个省的数据都封装入列表内
data_list = []
for province_data in province_data_list:
    province_name = province_data["name"] + "省"
    province_confirm = province_data["total"]["confirm"]
    data_list.append((province_name, province_confirm))

print(data_list)
# 创建地图对象
map = Map()

# 添加数据
map.add("各省份确诊人数", data_list, "china")

map.set_global_opts(
    title_opts=TitleOpts(title="全国疫情地图"),
    visualmap_opts=VisualMapOpts(
        is_show=True,
        is_piecewise=True,
        pieces=[
            {"min":1, "max":99, "label":"1-99人","color":"#CCFFFF"},
            {"min":100, "max":999, "label":"100-999人","color":"#FFFF99"},
            {"min":1000, "max":4999, "label":"1000-4999人","color":"#FF9966"},
            {"min":5000, "max":9999, "label":"5000-9999人","color":"#FF6666"},
            {"min":10000, "max":99999, "label":"10000-99999人","color":"#CC3333"},
            {"min":100000, "label":"10000+","color":"#990033"}
        ]
    )
)

# 绘图
map.render("全国疫情地图.html")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49