地图上显示中国各省份名称及相对应的数据(存于excel中,数据可自己定义读取),随鼠标移动显示;点击某省份能够进入该省份的地图,并且显示每个市/区名称以及各市/区对应的数据。
import pandas as pd
from pyecharts.charts import Map
from pyecharts import options as opts
from pyecharts.globals import ThemeType
Python轻松实现地图可视化(附详细源码)
地图点击省跳转到市_用Python来绘制自己的个人足迹地图,精确到市级别
data = pd.read_excel('GDP.xlsx')
province = list(data["province"])
clss = list(data["class"])
list = [list(z) for z in zip(province, clss)]
# maptype='china' 只显示全国直辖市和省级
map = Map(init_opts=opts.InitOpts(width="1500px", height="800px", theme=ThemeType.VINTAGE)) # 添加主题
map.set_global_opts(
# 标题配置项
title_opts=opts.TitleOpts(
title="等级分布地图",
pos_left="center"
),
# 图例配置项
legend_opts=opts.LegendOpts(
is_show=True,
pos_left="left",
),
# 视觉影射配置项
visualmap_opts=opts.VisualMapOpts(
min_=1, # 组件最小值
max_=32,
range_text = ['等级程度分布颜色区间:', ''], # 两端文本名称
is_piecewise=True, # 定义图例为分段型,默认为连续的图例
pos_top= "middle", # 组件离容器左侧的距离
pos_left="left",
orient="vertical", # 布局方式为垂直布局,水平为horizon
split_number=5 # 分成5个区间
)
)
map.add("程度", list, maptype="china")
map.render("全国.html")
同样的,读取各省份名称,一键生成
# province同上,是一个由省份名称组成的列表,如['河南', '山西']
# 切记,不要带省,只要名字就行
for i in province:
province_city = (
Map(init_opts=opts.InitOpts(width="1500px", height="800px", theme=ThemeType.VINTAGE))
.add("",
list, # 可以自己写一个字典写各个省份下的市所对应的数据,在这里只是一个range()
i)
.set_global_opts(
title_opts=opts.TitleOpts(title=i + "地图"),
visualmap_opts=opts.VisualMapOpts(
min_=1,
max_=17,
is_piecewise=True
)
)
.render(path=i + "地图.html")
)
切记,list中的城市名称一定要与地图上显示的名称对应,否则相对应的数据会显示不出来
chart_775f962500534f508fafa003f08442f9.on('click', function (param){
var selected = param.name;
if (selected) {
switch(selected){
case '北京':
location.href = "./北京地图.html";
break;
case '上海':
location.href = "./上海地图.html";
break;
case '天津':
location.href = "./天津地图.html";
break;
case '四川':
location.href = "./四川地图.html";
break;
case '安徽':
location.href = "./安徽地图.html";
break;
case '山东':
location.href = "./山东地图.html";
break;
case '江苏':
location.href = "./江苏地图.html";
break;
case '江西':
location.href = "./江西地图.html";
break;
case '河北':
location.href = "./河北地图.html";
break;
case '浙江':
location.href = "./浙江地图.html";
break;
case '海南':
location.href = "./海南地图.html";
break;
case '湖北':
location.href = "./湖北地图.html";
break;
case '湖南':
location.href = "./湖南地图.html";
break;
case '广东':
location.href = "./广东地图.html";
break;
case '福建':
location.href = "./福建地图.html";
break;
case '甘肃':
location.href = "./甘肃地图.html";
break;
case '广西':
location.href = "./广西地图.html";
break;
case '贵州':
location.href = "./贵州地图.html";
break;
case '河南':
location.href = "./河南地图.html";
break;
case '黑龙江':
location.href = "./黑龙江地图.html";
break;
case '内蒙古':
location.href = "./内蒙古地图.html";
break;
case '吉林':
location.href = "./吉林地图.html";
break;
case '辽宁':
location.href = "./辽宁地图.html";
break;
case '宁夏':
location.href = "./宁夏地图.html";
break;
case '青海':
location.href = "./青海地图.html";
break;
case '山西':
location.href = "./山西地图.html";
break;
case '陕西':
location.href = "./陕西地图.html";
break;
case '台湾':
location.href = "./台湾地图.html";
break;
case '西藏':
location.href = "./西藏地图.html";
break;
case '新疆':
location.href = "./新疆地图.html";
break;
case '云南':
location.href = "./云南地图.html";
break;
case '重庆':
location.href = "./重庆地图.html";
break;
default:
break;
}
}
});
首先打开"全国.html"的源代码,找到这个id
把上面那一段代码中的chart_后面的一串数字改成自己生成的地图id,将这段代码添加到代码段中的最后(一定不要放在最前面,会什么都显示不出来),然后点击保存就可以啦。