python 数据可视化- 地图

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

# 读取数据文件
f = open("D:/桌面/python/资料/可视化案例数据/地图数据/疫情.txt", "r", 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"]  # 省份名称
    print(province_name)
    province_name = ("%s省" % province_name)
    if province_name == "北京省":
        province_name = province_name.replace("省", "市")
    if province_name == "重庆省":
        province_name = province_name.replace("省", "市")
    if province_name == "天津省":
        province_name = province_name.replace("省", "市")
    if province_name == "内蒙古省":
        province_name = province_name.replace("省", "自治区")
    if province_name == "新疆省":
        province_name = province_name.replace("省", "维吾尔自治区")
    if province_name == "西藏省":
        province_name = province_name.replace("省", "自治区")
    if province_name == "广西省":
        province_name = province_name.replace("省", "壮族自治区")
    if province_name == "宁夏省":
        province_name = province_name.replace("省", "回族自治区")

    province_confirm = province_data["total"]["confirm"]  # 确诊人数
    data_list.append([province_name, province_confirm])

# 创建地图对象
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": "100000- 人", "color": "#990033"}
        ]
    )
)
# 绘图
map.render("全国疫情地图.html")

图片示例:
python 数据可视化- 地图_第1张图片

省级地图示例:

import json
from pyecharts.charts import Map
from pyecharts.options import *
# 读取文件
f = open("D:/桌面/python/资料/可视化案例数据/地图数据/疫情.txt", "r", encoding="UTF-8")
data = f.read()
# 关闭文件
f.close()
# 获取安徽省数据
# json 数据转化为python字典
data_dict = json.loads(data)
# 获取安徽省数据
data_list = []
cities_data = data_dict["areaTree"][0]["children"][31]["children"]
for city_data in cities_data:
    city_name = city_data["name"]
    city_name = ("%s市" % city_name)
    city_confirm = city_data["total"]["confirm"]
    # 准备数据为元组并放入list
    data_list.append((city_name, city_confirm))
print(data_list)

# 手动添加济源市数据
# data_list.append(("济源市", 5))



# 构建地图
map = Map()
map.add("安徽省疫情分布", data_list, "安徽")
# 设置全局选项
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": "100000- 人", "color": "#990033"}
        ]
    )
)

# 绘图
map.render("安徽省疫情地图.html")

python 数据可视化- 地图_第2张图片

你可能感兴趣的:(Python,信息可视化,python,数据分析)