Python Basemap加载SHP并填充颜色

Basemap是一个很好用的地图包,但是Basemap的水陆边界比较粗糙,当需要放大局部区域时通常不美观。

问题

1、如何用Basemap加载shp文件
2、对加载的shp面文件着色

解决方法

1、Basemap加载shp文件

示例数据获取地址:https://gadm.org/download_country_v3.html,随便下载一个国家的shp,我这里随便用了阿尔及利亚的数据。另,如果进不去网站,可能需要科学上网
Basemap加载shp文件不能直接进行着色,只能修改线的颜色,对面shp着色比较麻烦。
首先,通过readshapefile函数,代码如下:

from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.patches import PathPatch

fig = plt.figure()
ax  = fig.add_subplot(111)
m= Basemap(llcrnrlon=-10, llcrnrlat=18, urcrnrlon=15, urcrnrlat=40, projection='lcc', lat_1=22, lat_2=33, lon_0=0)
#设置陆地、海岸线等
m.drawmapboundary(fill_color='aqua')
m.fillcontinents(color='#ddaa66',lake_color='aqua')
m.drawcoastlines()
#设置经纬度
m.drawmeridians(np.arange(-180, 180, 5),\
                linewidth=0.5, fontsize=10, labels=[0,0,0,1],color='silver')
m.drawparallels(np.arange(-90, 90, 5),rotation=0,\
                linewidth=0.5, fontsize=10, labels=[1,0,0,0],color='silver')
#加载shp
m.readshapefile('gadm36_DZA_0','a', drawbounds=True,color='k',linewidth=1)#shp文件不带后缀
'''
m.readshapefile参数详解:
参数1:shp路径
参数2:对加载的shp的命名,可以随意设置(个人理解)
drawbounds:是否绘制shp边界,默认为True
linewidth:shp边界的线宽
color:边界线的颜色
antialiased:抗锯齿
ax:绘图位置
'''

得到结果如下
Python Basemap加载SHP并填充颜色_第1张图片

可以看到,我们想要的区域没有颜色标识,因为readshapefile没有设置shp面文件填充的选项,因此需要另外设置。

2、shp面文件颜色填充

先上代码

patches = [Polygon(np.array(shape), True) for info, shape in zip(m.a_info, m.a)]
ax.add_collection(PatchCollection(patches, facecolor= 'g', edgecolor='r', linewidths=1., zorder=2))
plt.show()
'''
facecolor为shp面颜色
edgecolor为线的颜色
'''

最后结果如下
Python Basemap加载SHP并填充颜色_第2张图片

最后完整代码如下

from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.patches import PathPatch

fig = plt.figure()
ax  = fig.add_subplot(111)

m= Basemap(llcrnrlon=-10, llcrnrlat=18, urcrnrlon=15, urcrnrlat=40, projection='lcc', lat_1=22, lat_2=33, lon_0=0)
m.drawmapboundary(fill_color='aqua')
m.fillcontinents(color='#ddaa66',lake_color='aqua')
m.drawcoastlines()

m.drawmeridians(np.arange(-180, 180, 5),\
                linewidth=0.5, fontsize=10, labels=[0,0,0,1],color='silver')
m.drawparallels(np.arange(-90, 90, 5),rotation=0,\
                linewidth=0.5, fontsize=10, labels=[1,0,0,0],color='silver')

m.readshapefile('gadm36_DZA_0','a', drawbounds=True,color='k',linewidth=1)

patches = [Polygon(np.array(shape), True) for info, shape in zip(m.a_info, m.a)]
        
ax.add_collection(PatchCollection(patches, facecolor= 'g', edgecolor='r', linewidths=1., zorder=2))

plt.show()

以上内容禁止转载,如有不当之处,敬请指正!

参考:
https://basemaptutorial.readthedocs.io/en/latest/shapefile.html
https://www.coder.work/article/4945725
https://zhuanlan.zhihu.com/p/348686158#:~:text=m.readshapefile%20%28%27gadm36_CHN_shp%2Fsimplied_china_country%27%2C%20%27china%27%2C%20drawbounds%3DTrue%29%20%23%20%E4%B8%AD%E5%9B%BD%EF%BC%8C%E6%B2%A1%E6%9C%89%E6%B9%BE%E6%B9%BE%20m.readshapefile,%28%27gadm36_CHN_shp%2Fgadm36_TWN_0%27%2C%20%27taiwan%27%2C%20drawbounds%3DTrue%29%20%23%20%E6%B9%BE%E6%B9%BE%EF%BC%8C%E6%98%AF%E4%B8%AD%E5%9B%BD%E7%9A%84%E4%B8%80%E9%83%A8%E5%88%86%20%E4%BA%8E%E6%98%AF%EF%BC%8C%E4%BB%8E%E6%96%B0%E6%89%BE%E4%BA%86%E4%B8%80%E4%B8%AA%E4%B8%AD%E5%9B%BD%E8%BD%AE%E5%BB%93%E6%95%B0%E6%8D%AE%E5%8C%85%EF%BC%8C%E8%BF%99%E9%87%8C%E9%9C%80%E8%A6%81%E5%90%90%E6%A7%BD%E4%B8%80%E4%B8%8B%EF%BC%8C%E4%B8%8D%E4%BB%85%E4%BB%85%E4%B8%8A%E9%9D%A2%E4%B8%8B%E8%BD%BD%E7%BD%91%E5%9D%80%EF%BC%8C%E7%94%9A%E8%87%B3CSDN%E4%B8%8A%E9%9D%A2%E5%BE%88%E5%A4%9A%E5%8C%85%E9%83%BD%E6%98%AF%E9%94%99%E7%9A%84%EF%BC%8CR%E4%BB%96%E5%A4%A7%E7%88%B7%EF%BC%9A%20%E8%99%BD%E7%84%B6%E4%B8%8D%E7%9F%A5%E9%81%93%E7%9F%A5%E4%B9%8E%E5%B0%8F%E7%BC%96%E6%98%AF%E6%80%8E%E4%B9%88%E5%8F%91%E7%8E%B0%E8%BF%99%E4%B8%AA%E9%97%AE%E9%A2%98%E7%9A%84%EF%BC%8C%E8%85%BE%E8%AE%AF%E8%A7%86%E9%A2%91%E5%8F%B7%E9%82%A3%E8%BE%B9%E5%B0%B1%E6%B2%A1%E6%9C%89%E5%8F%91%E7%8E%B0%EF%BC%8C%E5%98%BF%E5%98%BF%E3%80%82

你可能感兴趣的:(##,Python遥感数据处理,python,matplotlib,开发语言)