1.数据准备
下载中国shapefile格式的资料 下载地址 IT虾米网
2.示例
绘制黑白地图
from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt # 绘制地图 m = Basemap(llcrnrlon=80.33, llcrnrlat=3.01, urcrnrlon=138.16, urcrnrlat=56.123, projection='lcc', lat_0 = 42.5,lon_0=120) m.drawcoastlines() # 海岸线 m.drawcountries(linewidth=1.5) # 国界线 plt.show()
说明:
Basemap常用参数
llcrnrlon 左下角地理经度
llcrnrlat 左下角地理纬度
urcrnrlon 右上角地理经度
urcrnrlat 右上角地理纬度
width 地图的投影单位宽度
height 投影单位中地图的高度
lon_0 地图中心的经度
lat_0 地图中心的纬度
projection 设置要使用的地图投影
常用投影
cyl Cylindrical Equidistant 圆柱形等距投影 merc Mercator 墨卡托投影 tmerc Transverse Mercator 横轴墨卡托投影 cea Cylindrical Equal Area 圆柱等面积投影 lcc Lambert Conformal 兰伯特正形投影 laea Lambert Azimuthal Equal Area 兰勃特方位等积投影 aeqd Azimuthal Equidistant 等距方位离投影 poly Polyconic 多圆锥投影 sinu Sinusoidal 正弦曲线投影 moll Mollweide 默尔威得投影 mbtfpq McBryde-Thomas Flat-Polar Quartic 麦克布赖德-托马斯平极四次投影
添加省界线
from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt # 绘制地图 m = Basemap(llcrnrlon=80.33, llcrnrlat=3.01, urcrnrlon=138.16, urcrnrlat=56.123, projection='lcc', lat_0 = 42.5,lon_0=120) m.drawcoastlines() # 海岸线 m.drawcountries(linewidth=1.5) # 国界线 # CHN_adm1的数据是中国各省区域 m.readshapefile(shapefile='gadm36_CHN_shp/gadm36_CHN_1', name='states', drawbounds=True) plt.show()
添加台湾,给各省上金色,北京红色
from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt from matplotlib.patches import Polygon ax = plt.gca() m = Basemap(llcrnrlon=80.33, llcrnrlat=3.01, urcrnrlon=138.16, urcrnrlat=56.123, projection='lcc', lat_0 = 42.5,lon_0=120) m.drawcoastlines() # 海岸线 m.drawcountries() # 国界线 # CHN_adm1的数据是中国各省区域 m.readshapefile(shapefile='gadm36_CHN_shp/gadm36_CHN_1', name='states', drawbounds=True) # 获取台湾省的 shp 信息 m.readshapefile('gadm36_TWN_shp/gadm36_TWN_0', 'taiwan', drawbounds=True) # 在形状上填充颜色——珊瑚色 for shp in m.states: poly = Polygon(xy=shp, facecolor='gold',edgecolor='b' ) ax.add_patch(poly) for shp in m.taiwan: poly = Polygon(shp, facecolor='gold',edgecolor='b') ax.add_patch(poly) # 用红色填充北京市 for info, shp in zip(m.states_info, m.states): proid = info['NAME_1'] if proid == 'Beijing': poly = Polygon(shp, facecolor='r', lw=3) ax.add_patch(poly) plt.show()
带各省轮廓的阴暗的浮雕图
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]) m = 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) m.readshapefile(shapefile='gadm36_CHN_shp/gadm36_CHN_1', name='states', drawbounds=True) for info, shp in zip(m.states_info, m.states): proid = info['NAME_1'] if proid == 'Beijing': poly = Polygon(shp,facecolor='r',edgecolor='c') ax1.add_patch(poly) m.shadedrelief() m.drawcoastlines() m.drawcountries() plt.show()
绘制地形图
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("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 m = Basemap(llcrnrlon=80.33, llcrnrlat=3.01, urcrnrlon=138.16, urcrnrlat=56.123, resolution='h', projection='cass', lat_0 = 42.5,lon_0=120) m.readshapefile(shapefile='gadm36_CHN_shp/gadm36_CHN_1', name='states', drawbounds=True) m.etopo() # 绘制地形图,浮雕样式 m.drawcoastlines() plt.show()
绘制地形图和首都
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("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 m = Basemap(llcrnrlon=80.33, llcrnrlat=3.01, urcrnrlon=138.16, urcrnrlat=56.123, resolution='h', projection='cass', lat_0 = 42.5,lon_0=120) m.readshapefile(shapefile='gadm36_CHN_shp/gadm36_CHN_1', name='states', drawbounds=True) m.etopo() # 绘制地形图,浮雕样式 m.drawcoastlines() x,y = m(lon[2],lat[2]) # 北京市坐标,经纬度坐标转换为该map的坐标 m.scatter(x,y,s=200,marker='*',facecolors='r',edgecolors='r') # 绘制首都 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_excel("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 # 绘制散点图时图形的大小 m = 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.) m.drawcoastlines() m.drawcountries(linewidth=1.5) m.readshapefile(shapefile='gadm36_CHN_shp/gadm36_CHN_1', name='states', drawbounds=True) m.drawmapboundary() parallels = np.arange(0.,90,10.) m.drawparallels(parallels,labels=[1,0,0,0],fontsize=10) # 绘制纬线 meridians = np.arange(80.,140.,10.) m.drawmeridians(meridians,labels=[0,0,0,1],fontsize=10) # 绘制经线 x,y = m(lon,lat) m.scatter(x,y,s=size) plt.title("Population Distribution in China") plt.show()
2015Cities-CHINA.xlsx
链接:https://pan.baidu.com/s/1E3ZnVvEl85QDWJfZZZeKng
提取码:052e
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/20441.html