# 利用Basemap绘制中国地图

## 利用Basemap绘制中国地图

（2）2015Cities-CHINA.xlsx ，包含中国各城市的经纬度，自己网上搜索整理，数据可能过时了，和维基百科查到的数据不太对，但是拿来写个小程序还是足够的） xlsx可以用excel打开查看一下：

basemap绘图常用函数：

basemap地图背景设置函数：

import time

start = time.clock()

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
fig = plt.figure()

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)

for info, shp in zip(map.states_info, map.states):
if proid == 'Guangdong':
poly = Polygon(shp,facecolor='g',edgecolor='c', lw=3) # 绘制广东省区域

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
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.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

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.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()