文章转自:http://blog.csdn.net/ouening/article/details/55227364
使用folium实现中国地图绘制,文章链接:
python/folium绘制中国人口数量热力图(HeatMap)
今天发现另一个软件库folium可以实现对openstreetmap的调用,参考链接http://blog.csdn.net/qq_14906811/article/details/74906275 ,下面是操作步骤:
pip3 install folium
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 23 20:19:47 2017
@author: 周文青
"""
import numpy as np
import pandas as pd
import seaborn as sns
import folium
import webbrowser
from folium.plugins import HeatMap
# posi=pd.read_csv("D:\\Files\\datasets\\CitiesLatLon_China.csv")
posi=pd.read_excel("2015Cities-CHINA.xlsx")
num = 10
lat = np.array(posi["lat"][0:num]) # 获取维度之维度值
lon = np.array(posi["lon"][0:num]) # 获取经度值
pop = np.array(posi["pop"][0:num],dtype=float) # 获取人口数,转化为numpy浮点型
gdp = np.array(posi["GDP"][0:num],dtype=float) # 获取人口数,转化为numpy浮点型
data1 = [[lat[i],lon[i],pop[i]] for i in range(num)] #将数据制作成[lats,lons,weights]的形式
map_osm = folium.Map(location=[35,110],zoom_start=5) #绘制Map,开始缩放程度是5倍
HeatMap(data1).add_to(map_osm) # 将热力图添加到前面建立的map里
file_path = r"D:\Files\python\地图\人口.html"
map_osm.save(file_path) # 保存为html文件
webbrowser.open(file_path) # 默认浏览器打开
这篇博文主要实现用Pyhon,Matplotlib/Basemap绘制中国地图,主要是各省份行政图(轮廓图),地形图和人口分布图,其中人口分布可以嵌入到上述图形中。
参考链接:
(1)https://www.zhihu.com/question/49669755
(2)http://basemaptutorial.readthedocs.io/en/latest/backgrounds.html#fillcontinents
1、数据准备:
(1)到http://www.gadm.org/download 下载中国shapefile格式的资料,有读者反应进去不了该网站,可以在我的github下下载https://github.com/ouening/python-code/tree/master/resources下载后的文件名为CHN_adm_shp.zip
,解压后如图:
主要用到的文件是CHN_adm1.shp
,另外CHM_adm1.csv
可以用notepad打开查看一下文件内容
(2)2015Cities-CHINA.xlsx ,包含中国各城市的经纬度,自己网上搜索整理,数据可能过时了,和维基百科查到的数据不太对,但是拿来写个小程序还是足够的)
xlsx可以用excel打开查看一下:
在python中可以导入pandas模块,使用read_excel()
函数方便读取文件
basemap绘图常用函数:
basemap地图背景设置函数:
更多详细的函数介绍请参考
http://matplotlib.org/basemap/users/geography.html
import time
start = time.clock()
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
fig = plt.figure()
ax1 = fig.add_axes([0.1,0.1,0.8,0.8])
map = Basemap(llcrnrlon=80.33,
llcrnrlat=3.01,
urcrnrlon=138.16,
urcrnrlat=56.123,
resolution='h', projection='cass', lat_0 = 42.5,lon_0=120,ax=ax1)
shp_info = map.readshapefile("D:\\GoogleDownload\\CHN_adm_shp\\CHN_adm1",'states',drawbounds=True) # CHN_adm1的数据是中国各省区域
for info, shp in zip(map.states_info, map.states):
proid = info['NAME_1'] # 可以用notepad打开CHN_adm1.csv文件,可以知道'NAME_1'代表各省的名称
if proid == 'Guangdong':
poly = Polygon(shp,facecolor='g',edgecolor='c', lw=3) # 绘制广东省区域
ax1.add_patch(poly)
map.shadedrelief() # 绘制阴暗的浮雕图
map.drawcoastlines()
end=time.clock()
print(end-start)
plt.show()
import time
start = time.clock()
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import pandas as pd
import numpy as np
posi=pd.read_excel("D:\\Files\\datasets\\2015Cities-CHINA.xlsx") #读取中国城市数据
lat = np.array(posi["lat"][0:120]) # 获取维度之维度值
lon = np.array(posi["lon"][0:120]) # 获取经度值
pop = np.array(posi["pop"][0:120],dtype=float)
size=(pop/np.max(pop))*100
map = Basemap(llcrnrlon=80.33,
llcrnrlat=3.01,
urcrnrlon=138.16,
urcrnrlat=56.123,
resolution='h', projection='cass', lat_0 = 42.5,lon_0=120)
map.readshapefile("D:\\GoogleDownload\\CHN_adm_shp\\CHN_adm1",'states',drawbounds=True)
map.etopo() # 绘制地形图,浮雕样式
map.drawcoastlines()
x,y = map(lon[2],lat[2]) # 北京市坐标,经纬度坐标转换为该map的坐标
a,b = map(lon,lat)
# map.scatter(a,b,s=size) # 取消注释此行即可获得中国各地区人口分布示意图
map.scatter(x,y,s=200,marker='*',facecolors='r',edgecolors='r') # 绘制首都
end=time.clock()
print(end-start)
plt.show()
import urllib
import numpy as np
import matplotlib
matplotlib.rcParams['toolbar'] = 'None'
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
from matplotlib.animation import FuncAnimation
import pandas as pd
import seaborn as sns
# posi=pd.read_csv("D:\\Files\\datasets\\CitiesLatLon_China.csv")
posi=pd.read_excel("D:\\Files\\datasets\\2015Cities-CHINA.xlsx")
lat = np.array(posi["lat"][0:120]) # 获取维度之维度值
lon = np.array(posi["lon"][0:120]) # 获取经度值
pop = np.array(posi["pop"][0:120],dtype=float) # 获取人口数,转化为numpy浮点型
gdp = np.array(posi["GDP"][0:120],dtype=float) # 获取人口数,转化为numpy浮点型
size=(pop/np.max(pop))*100 # 绘制散点图时图形的大小,如果之前pop不转换为浮点型会没有大小不一的效果
# size=(gdp/np.max(gdp))*100 # 绘制散点图时图形的大小,如果之前pop不转换为浮点型会没有大小不一的效果
map = Basemap(projection='stere',
lat_0=35, lon_0=110,
llcrnrlon=82.33,
llcrnrlat=3.01,
urcrnrlon=138.16,
urcrnrlat=53.123,resolution='l',area_thresh=10000,rsphere=6371200.)
map.drawcoastlines()
map.drawcountries()
map.drawcounties()
map.readshapefile("D:\\GoogleDownload\\CHN_adm_shp\\CHN_adm1",'states',drawbounds=True)
map.drawmapboundary()
parallels = np.arange(0.,90,10.)
map.drawparallels(parallels,labels=[1,0,0,0],fontsize=10) # 绘制纬线
meridians = np.arange(80.,140.,10.)
map.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10) # 绘制经线
x,y = map(lon,lat)
# map.scatter(x,y,edgecolors='r',facecolors='r',marker='*',s=320)
map.scatter(x,y,s=size)
plt.title("Population Distribution in China")
plt.show()