Python基础知识:整理14 利用pyecharts生成地图

发布时间:2024年01月14日

1 地图可视化的基本使用

from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts
# 准备地图对象
map = Map()

# 准备数据
data = [("北京市", 8), ("上海市", 99), ("广州省", 199), ("重庆市", 400), ("浙江省", 500), ("江苏省", 600)]

# 添加数据
map.add("测试地图", data, "china")  # 第一个参数是名称; 第二个参数是数据;第三个参数是地图类型,默认是china

# 设置全局配置
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": "#0000CD"},
            {"min": 100, "max": 999, "label": "100-999", "color": "#FFF000"}
        ]
    )
)

# 绘图
map.render("./modules/map_china.html")

?

以下案例所用数据没有可信度,只用来学习参考

?

2?全国疫情地图构建案例


import json
from pyecharts.charts import Map
from pyecharts.options import *

# 读取数据
fr = open("./text/疫情.txt", "r", encoding='utf-8')
data = fr.read()
fr.close()

# 取到各省的数据
# 将字符串json转换为python的字典、
data_dict = json.loads(data)
# 从字典中取出各省的数据
provinces_data_lists = data_dict['areaTree'][0]['children']

# 组装每个省份和确诊人数为元组,并将各个省的数据都封装到列表内
data_list = []   # 绘图需要用到的数据列表
for provinces_data in provinces_data_lists:
    province_name = provinces_data['name']  # 省份名称
    province_total_confirm = provinces_data['total']['confirm']  # 确诊人数
    data_list.append((province_name+"省", province_total_confirm))

# print(data_list)
map = Map()
map.add("各省份确诊人数", data_list, "china")

# 设置配置项
map.set_global_opts(
    title_opts=TitleOpts(title="2020年01月01日全国疫情确诊人数"),
    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": "#0000CD"},
            {"min": 1000, "max": 4999, "label": "1000-4999", "color": "#FFF000"},
            {"min": 5000, "max": 9999, "label": "1000-9999", "color": "#9ACD32"},
            {"min": 10000, "max": 99999, "label": "10000-99999", "color": "#CD5C5C"},
            {"min": 100000, "label": "100000", "color": "#FF00FF"}
        ]
    )
)
map.render("./modules/map_yiqin_china.html")


?

3 江苏疫情地图构建案例


import json
from pyecharts.charts import Map
from pyecharts.options import *

# 读取数据
fr = open("./text/疫情.txt", "r", encoding='utf-8')
data = fr.read()
fr.close()

data_dict = json.loads(data)
city_data_lists = data_dict['areaTree'][0]['children'][1]["children"]

data_list = []
for city_data in city_data_lists:
    city_name = city_data['name'] + "市"
    city_total_confirm = city_data['total']['confirm']

    data_list.append((city_name, city_total_confirm))
# print(data_list)

map = Map()
map.add("江苏省疫情分布图", data_list, "江苏")

map.set_global_opts(
    title_opts=TitleOpts(title="2020年01月01日江苏省疫情确诊人数"),
    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": "#0000CD"},
            {"min": 1000, "max": 4999, "label": "1000-4999", "color": "#FFF000"},
            {"min": 5000, "max": 9999, "label": "1000-9999", "color": "#9ACD32"},
            {"min": 10000, "max": 99999, "label": "10000-99999", "color": "#CD5C5C"},
            {"min": 100000, "label": "100000", "color": "#FF00FF"}
        ]

    )
)

map.render("./modules/map_yiqin_jiangsu.html")

文章来源:https://blog.csdn.net/m0_61495539/article/details/135584136
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。