python_day7_map

绘制简易地图

导包

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()

python_day7_map_第1张图片

案例1,全国疫情地图

python_day7_map_第2张图片
python_day7_map_第3张图片
python_day7_map_第4张图片

读取数据文件

f = open("D:/疫情.txt", "r", encoding="utf-8")
data = f.read()
# 关闭文件
f.close()
print(type(data))

在这里插入图片描述

将json转为字典

data_dict = json.loads(data)
# print(data_dict)
print(type(data_dict))

在这里插入图片描述

取出各省数据

# print(data_dict["areaTree"][0]["children"])
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"]
    # (省份,确诊人数)组成元组,使用append()方法加入列表,(name,num)为列表中的一个元素
    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")

python_day7_map_第5张图片
python_day7_map_第6张图片

案例二,取出某省份数据,绘制疫情地图

python_day7_map_第7张图片

# 导包
import json
from pyecharts.charts import Map
from pyecharts.options import *

# 取数据
f = open("D:/疫情.txt", "r", encoding="utf-8")
data = f.read()
f.close()
# print(data)
# print(type(data))

# json转python数据类型
data_dict = json.loads(data)
# print(data_dict)
# print(type(data_dict))

# 取出江苏省数据
# print(data_dict['areaTree'][0]['children'][1])
jiangsu_data = data_dict['areaTree'][0]['children'][1]
data_list = jiangsu_data['children']
# print(data_list)

# 取出各市数据
list_city = []
for element in data_list:
    name = element['name']  # 市名,  str+"市"
    num = element['total']['confirm']
    list_city.append((name, num))  # 列表中每个元素为元组
# print(list_shi)

# 绘图
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")

python_day7_map_第8张图片
python_day7_map_第9张图片

你可能感兴趣的:(python,python,windows,开发语言)