本文共 3903 字,大约阅读时间需要 13 分钟。
译注:绘制地图时因为一些原因可能需要使用shapefile添加地图信息,比如很多软件中的关于中国的地图信息都不是很准确,当要明确标注中国边界信息时就会出问题。这次就介绍一下如何利用shp文件添加地图信息。
基本用法:
首先用一种最简单的方式绘制 shapefile
from mpl_toolkits.basemap import Basemapimport matplotlib.pyplot as pltmap = Basemap(llcrnrlon=-0.5,llcrnrlat=39.8,urcrnrlon=4.,urcrnrlat=43., resolution='i', projection='tmerc', lat_0 = 39.5, lon_0 = 1)map.drawmapboundary(fill_color='aqua')map.fillcontinents(color='#ddaa66',lake_color='aqua')map.drawcoastlines()map.readshapefile('../sample_files/comarques', 'comarques')plt.show()
注意:
如图所示,结果仅是 polygons(polylines)的边界。如果要填充 polygons,可看后面的 填充 polygons 部分。
读取点数据
绘制点要复杂一些。首先,要读取 shapefile,然后使用 scatter, plot 或 matplotlib 函数进行绘制。
from mpl_toolkits.basemap import Basemapimport matplotlib.pyplot as pltmap = Basemap(llcrnrlon=-0.5,llcrnrlat=39.8,urcrnrlon=4.,urcrnrlat=43., resolution='i', projection='tmerc', lat_0 = 39.5, lon_0 = 1)map.drawmapboundary(fill_color='aqua')map.fillcontinents(color='#ddaa66',lake_color='aqua')map.drawcoastlines()map.readshapefile('../sample_files/comarques', 'comarques')lightning_info = map.readshapefile('../sample_files/lightnings', 'lightnings')print lightning_infofor info, lightning in zip(map.lightnings_info, map.lightnings): if float(info['amplitude']) < 0: marker = '_' else: marker = '+' map.plot(lightning[0], lightning[1], marker=marker, color='m', markersize=8, markeredgewidth=2)plt.show()
例子显示了一次雷暴过程中Catalonia 发生的闪电位置。
多边形信息
此例展示了如何使用 shapefile 属性选择一些 geometries
from mpl_toolkits.basemap import Basemapimport matplotlib.pyplot as pltmap = Basemap(llcrnrlon=-0.5,llcrnrlat=39.8,urcrnrlon=4.,urcrnrlat=43., resolution='i', projection='tmerc', lat_0 = 39.5, lon_0 = 1)map.drawmapboundary(fill_color='aqua')map.fillcontinents(color='#ddaa66',lake_color='aqua')map.drawcoastlines()map.readshapefile('../sample_files/comarques', 'comarques', drawbounds = False)for info, shape in zip(map.comarques_info, map.comarques): if info['nombre'] == 'Selva': x, y = zip(*shape) map.plot(x, y, marker=None,color='m')plt.show()
填充多边形
基本的绘制并不会填充多边形,下面介绍以下如何绘制填充多边形:
from mpl_toolkits.basemap import Basemapimport matplotlib.pyplot as pltfrom matplotlib.patches import Polygonfrom matplotlib.collections import PatchCollectionfrom matplotlib.patches import PathPatchimport numpy as npfig = plt.figure()ax = fig.add_subplot(111)map = Basemap(llcrnrlon=-0.5,llcrnrlat=39.8,urcrnrlon=4.,urcrnrlat=43., resolution='i', projection='tmerc', lat_0 = 39.5, lon_0 = 1)map.drawmapboundary(fill_color='aqua')map.fillcontinents(color='#ddaa66',lake_color='aqua')map.drawcoastlines()map.readshapefile('../sample_files/comarques', 'comarques', drawbounds = False)patches = []for info, shape in zip(map.comarques_info, map.comarques): if info['nombre'] == 'Selva': patches.append( Polygon(np.array(shape), True) ) ax.add_collection(PatchCollection(patches, facecolor= 'm', edgecolor='k', linewidths=1., zorder=2))plt.show()
http://shapelib.maptools.org/shp_api.html
http://stackoverflow.com/questions/15968762/shapefile-and-matplotlib-plot-polygon-collection-of-shapefile-coordinates转载地址:http://wfmzi.baihongyu.com/