diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/dbnavigator.xml b/.idea/dbnavigator.xml new file mode 100644 index 0000000..3e1b29d --- /dev/null +++ b/.idea/dbnavigator.xml @@ -0,0 +1,454 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..15a15b2 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..ffe2a51 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,19 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..e524f65 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..c5b76fb --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/personal_travel_map.iml b/.idea/personal_travel_map.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/personal_travel_map.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 327f8fa..075dba5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,241 @@ -# personalTravelMap +# Python之绘制个人足迹地图 + + + +前两年,足迹地图小程序风靡朋友圈,一时间大家都流行晒自己的旅行地图。但是,笔者最近体验了好几款足迹地图的小程序,发现这些小程序虽然号称是足迹地图,但最多只是展示到省级别,无法精确到市级别,因此,笔者周末花了点时间,用Python来绘制自己的个人足迹地图,可以精确到市级别。 +  下面的部分,笔者将介绍如何简单地来绘制个人足迹地图。 +  首先我们需要安装以下Python的第三方模块: + +```python +echarts-china-cities-pypkg==0.0.9 +echarts-china-provinces-pypkg==0.0.3 +pyecharts==1.6.2 +PyYAML==5.3.1 +``` + +本项目的完整结构如下: + +![完整的项目结构](https://img-blog.csdnimg.cn/20200531105908178.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2pjbGlhbjkx,size_16,color_FFFFFF,t_70) + +我们在项目中采用YAML文件来配置自己旅行过的省以及省下面的市,笔者的配置文件如下: + +```tcl +北京: + - 延庆区 + - 昌平区 + - 海淀区 + - 东城区 + - 西城区 + - 大兴区 + - 朝阳区 +上海: + - 嘉定区 + - 青浦区 + - 松江区 + - 宝山区 + - 闵行区 + - 奉贤区 + - 浦东新区 + - 崇明区 + - 徐汇区 + - 黄浦区 + - 静安区 + - 杨浦区 + - 虹口区 + - 金山区 + - 长宁区 + - 普陀区 +天津: + - 滨海新区 + - 南开区 + - 河西区 + - 和平区 +河北: + - 保定市 + - 廊坊市 +山东: + - 烟台市 + - 青岛市 +江苏: + - 南京市 + - 苏州市 + - 扬州市 + - 镇江市 + - 无锡市 +浙江: + - 杭州市 + - 绍兴市 + - 宁波市 + - 舟山市 + - 湖州市 + - 嘉兴市 + - 台州市 + - 金华市 +江西: + - 南昌市 + - 九江市 +安徽: + - 黄山市 +湖北: + - 武汉市 +湖南: + - 长沙市 +海南: + - 海口市 + - 三亚市 +四川: + - 成都市 +广东: + - 湛江市 +``` + +接着我们利用Python脚本来生成足迹地图。先是全国地图,用于显示省和直辖市的情况,`china_map.py`的代码如下: + +```python +# -*- coding: utf-8 -*- +# author: Jclian91 +# place: Pudong Shanghai +# time: 2020/5/31 9:21 上午 +# 绘制个人足迹地图:全国范围 +import yaml +from pyecharts.charts import Map +from pyecharts import options as opts + +# 省和直辖市 +with open("travel_config.yml", 'r', encoding='utf-8') as ymlfile: + province_city_dict = yaml.safe_load(ymlfile) + +province_dict = dict(zip(province_city_dict.keys(), [1]*len(province_city_dict.keys()))) +print(province_dict) + +# maptype='china' 只显示全国直辖市和省级 +map = Map(init_opts=opts.InitOpts(width='1200px', height='800px')) +map.set_global_opts( + title_opts=opts.TitleOpts(title="个人足迹地图"), + visualmap_opts=opts.VisualMapOpts(max_=1, is_piecewise=True, + pieces=[ + {"max": 1, "min": 1, "label": "去过", "color": "#4EA397"}, + {"max": 0, "min": 0, "label": "未去过", "color": "#FFFFFF"}, + ]) #最大数据范围,分段 + ) +map.add("个人足迹地图", data_pair=province_dict.items(), maptype="china", is_roam=True) +map.render('全国.html') + +``` + +运行上述脚本,会生成`全国.html`。为了保证图片居中显示,我们对该HTML中的下面一行: + +```html +
+``` + +的样式(style)增加如下内容: + +```html +margin:auto;top:30px +``` + +全国范围的足迹地图效果图如下: + +![个人足迹地图——全国](https://img-blog.csdnimg.cn/20200531110511512.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2pjbGlhbjkx,size_16,color_FFFFFF,t_70#pic_center) + +接着,我们来生成每个省市的足迹地图,利用`province_map.py`脚本,代码如下: + +```python +# -*- coding: utf-8 -*- +# author: Jclian91 +# place: Pudong Shanghai +# time: 2020/5/31 10:01 上午 +# 绘制个人足迹地图:省市范围 +import yaml +from pyecharts.charts import Map +from pyecharts import options as opts + +# 省和直辖市下面的市 +with open("travel_config.yml", 'r', encoding='utf-8') as ymlfile: + province_city_dict = yaml.safe_load(ymlfile) + + +for province, cities in province_city_dict.items(): + + map = Map(init_opts=opts.InitOpts(width='1200px', height='800px')) + map.set_global_opts( + title_opts=opts.TitleOpts(title="个人足迹地图-%s" % province), + visualmap_opts=opts.VisualMapOpts(max_=1, is_piecewise=True, + pieces=[ + {"max": 1, "min": 1, "label": "去过", "color": "#4EA397"}, + {"max": 0, "min": 0, "label": "未去过", "color": "#FFFFFF"}, + ]) #最大数据范围,分段 + ) + city_dict = dict(zip(cities, [1]*len(cities))) + map.add("个人足迹地图-%s" % province, data_pair=city_dict.items(), maptype=province, is_roam=True) + map.render('./provinces/%s.html' % province) + print("生成个人足迹地图-%s 成功" % province) +``` + +这时,会在`provinces`目录下生成每个省或者直辖市的足迹地图,我们分别对每个HTML进行修改,保证图片居中。我们在这里只展示三个省市的情况: + +![个人足迹地区——海南](https://img-blog.csdnimg.cn/20200531110800239.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2pjbGlhbjkx,size_16,color_FFFFFF,t_70#pic_center) + +![个人足迹地图——江苏](https://img-blog.csdnimg.cn/20200531110819229.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2pjbGlhbjkx,size_16,color_FFFFFF,t_70#pic_center) + +![个人足迹地图——浙江](https://img-blog.csdnimg.cn/20200531110836218.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2pjbGlhbjkx,size_16,color_FFFFFF,t_70#pic_center) + +最后一步,也是最为关键的一步,就是我们要打通全国足迹地图和每个省市足迹地图的联系,也就是说,我们点击全国足迹地图上去过的省份,那么可以点击进去查看该省市下的足迹地图情况,比如说去过哪些市。 +  对此,我们修改生成的全国.html文件,假设该地图的图像句柄为chart_a438407081634ceab6af1c965034ae16,那么在最后的 + + + + +
+ + + diff --git a/provinces/北京.html b/provinces/北京.html new file mode 100644 index 0000000..c601ba0 --- /dev/null +++ b/provinces/北京.html @@ -0,0 +1,173 @@ + + + + + Awesome-pyecharts + + + + + +
+ + + diff --git a/provinces/四川.html b/provinces/四川.html new file mode 100644 index 0000000..e3d01a7 --- /dev/null +++ b/provinces/四川.html @@ -0,0 +1,145 @@ + + + + + Awesome-pyecharts + + + + + +
+ + + diff --git a/provinces/天津.html b/provinces/天津.html new file mode 100644 index 0000000..ed556d1 --- /dev/null +++ b/provinces/天津.html @@ -0,0 +1,157 @@ + + + + + Awesome-pyecharts + + + + + +
+ + + diff --git a/provinces/安徽.html b/provinces/安徽.html new file mode 100644 index 0000000..fa7c4aa --- /dev/null +++ b/provinces/安徽.html @@ -0,0 +1,145 @@ + + + + + Awesome-pyecharts + + + + + +
+ + + diff --git a/provinces/山东.html b/provinces/山东.html new file mode 100644 index 0000000..7eb7333 --- /dev/null +++ b/provinces/山东.html @@ -0,0 +1,149 @@ + + + + + Awesome-pyecharts + + + + + +
+ + + diff --git a/provinces/广东.html b/provinces/广东.html new file mode 100644 index 0000000..ebe3004 --- /dev/null +++ b/provinces/广东.html @@ -0,0 +1,145 @@ + + + + + Awesome-pyecharts + + + + + +
+ + + diff --git a/provinces/江苏.html b/provinces/江苏.html new file mode 100644 index 0000000..ceaa7c5 --- /dev/null +++ b/provinces/江苏.html @@ -0,0 +1,161 @@ + + + + + Awesome-pyecharts + + + + + +
+ + + diff --git a/provinces/江西.html b/provinces/江西.html new file mode 100644 index 0000000..76cd38d --- /dev/null +++ b/provinces/江西.html @@ -0,0 +1,149 @@ + + + + + Awesome-pyecharts + + + + + +
+ + + diff --git a/provinces/河北.html b/provinces/河北.html new file mode 100644 index 0000000..4424731 --- /dev/null +++ b/provinces/河北.html @@ -0,0 +1,149 @@ + + + + + Awesome-pyecharts + + + + + +
+ + + diff --git a/provinces/浙江.html b/provinces/浙江.html new file mode 100644 index 0000000..dea2f6f --- /dev/null +++ b/provinces/浙江.html @@ -0,0 +1,173 @@ + + + + + Awesome-pyecharts + + + + + +
+ + + diff --git a/provinces/海南.html b/provinces/海南.html new file mode 100644 index 0000000..29f5236 --- /dev/null +++ b/provinces/海南.html @@ -0,0 +1,149 @@ + + + + + Awesome-pyecharts + + + + + +
+ + + diff --git a/provinces/湖北.html b/provinces/湖北.html new file mode 100644 index 0000000..3560512 --- /dev/null +++ b/provinces/湖北.html @@ -0,0 +1,145 @@ + + + + + Awesome-pyecharts + + + + + +
+ + + diff --git a/provinces/湖南.html b/provinces/湖南.html new file mode 100644 index 0000000..d3927c3 --- /dev/null +++ b/provinces/湖南.html @@ -0,0 +1,145 @@ + + + + + Awesome-pyecharts + + + + + +
+ + + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7b581a4 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +pyecharts==1.6.2 +PyYAML==5.3.1 +echarts-china-cities-pypkg==0.0.9 +echarts-china-provinces-pypkg==0.0. \ No newline at end of file diff --git a/travel_config.yml b/travel_config.yml new file mode 100644 index 0000000..c15571b --- /dev/null +++ b/travel_config.yml @@ -0,0 +1,68 @@ +北京: + - 延庆区 + - 昌平区 + - 海淀区 + - 东城区 + - 西城区 + - 大兴区 + - 朝阳区 + - 丰台区 +上海: + - 嘉定区 + - 青浦区 + - 松江区 + - 宝山区 + - 闵行区 + - 奉贤区 + - 浦东新区 + - 崇明区 + - 徐汇区 + - 黄浦区 + - 静安区 + - 杨浦区 + - 虹口区 + - 金山区 + - 长宁区 + - 普陀区 +天津: + - 滨海新区 + - 南开区 + - 河西区 + - 和平区 +河北: + - 保定市 + - 廊坊市 +山东: + - 烟台市 + - 青岛市 +江苏: + - 南京市 + - 苏州市 + - 扬州市 + - 镇江市 + - 无锡市 +浙江: + - 杭州市 + - 绍兴市 + - 宁波市 + - 舟山市 + - 湖州市 + - 嘉兴市 + - 台州市 + - 金华市 +江西: + - 南昌市 + - 九江市 +安徽: + - 黄山市 +湖北: + - 武汉市 +湖南: + - 长沙市 +海南: + - 海口市 + - 三亚市 +四川: + - 成都市 +广东: + - 湛江市 \ No newline at end of file diff --git a/个人足迹_中国.png b/个人足迹_中国.png new file mode 100644 index 0000000..39afe58 Binary files /dev/null and b/个人足迹_中国.png differ diff --git a/个人足迹地图_江苏.png b/个人足迹地图_江苏.png new file mode 100644 index 0000000..4c35e88 Binary files /dev/null and b/个人足迹地图_江苏.png differ diff --git a/个人足迹地图_浙江.png b/个人足迹地图_浙江.png new file mode 100644 index 0000000..2cb7f55 Binary files /dev/null and b/个人足迹地图_浙江.png differ diff --git a/个人足迹地图_海南.png b/个人足迹地图_海南.png new file mode 100644 index 0000000..28ba13a Binary files /dev/null and b/个人足迹地图_海南.png differ diff --git a/全国.html b/全国.html new file mode 100644 index 0000000..13a9ff8 --- /dev/null +++ b/全国.html @@ -0,0 +1,249 @@ + + + + + Awesome-pyecharts + + + + + +
+ + +