基础:
- # 可视化图表,导包(地图)
- from pyecharts.charts import Map
- from pyecharts.options import VisualMapOpts
-
- # 生成地图对象
- map = Map()
-
- # 准备数据--------------元组
- data = [
- ("北京市", 99),
- ("上海市", 199),
- ("湖南省", 299),
- ("台湾省", 399),
- ("广东省", 499)
- ]
-
- # 添加数据
- map.add("测试地图", data, "china")
-
- # 设置全局选项
- map.set_global_opts(
- visualmap_opts=VisualMapOpts(
- is_calculable=True,
- is_piecewise=True,
- pieces=[
- {"min": 1, "max": 9, "label": "1-9", "color": "#CCFFFF"},
- {"min": 10, "max": 99, "label": "10-99", "color": "#FF6666"},
- {"min": 100, "max": 500, "label": "100-500", "color": "#990033"},
- ]
- )
- )
-
- # 绘图
- map.render()
全国:
- import json
- from pyecharts.charts import Map
- from pyecharts.options import *
-
- # 打开、读取、关闭文件
- f = open("./疫情.txt", "r", encoding="UTF-8")
- data = f.read()
- f.close()
-
- # JSON转换为Python
- data_dict = json.loads(data)
-
- # 取出省份
- province_data_dict = data_dict["areaTree"][0]["children"]
-
- # 每个省份
- data_list = []
- for province_data in province_data_dict:
- 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_calculable=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")
省份:
- # 导包
- import json
- from pyecharts.charts import Map
- from pyecharts.options import *
-
- # 打开文件
- f = open("./疫情.txt", "r", encoding="UTF-8")
- data = f.read()
- f.close()
-
- # JSON 转 Python
- data_dict = json.loads(data)
-
- # 准备河南省数据
- data_henan = data_dict["areaTree"][0]["children"][3]["children"]
- # print(data_henan)
-
- # 每个市的数据
- data_list = []
- for data_henan_num in data_henan:
- # print(data_henan_num)
- data_name = data_henan_num["name"] + "市"
- data_confirm = data_henan_num["total"]["confirm"]
- data_list.append((data_name, data_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_calculable=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")