python指南针_用形状文件或地质指南针绘制遮住的南极洲

本文介绍了如何使用Python的Basemap和Shapely库来绘制南极洲地图,并通过形状文件和指南针显示遮盖效果。示例代码展示了如何处理地图边界、限制轮廓,并将地图与特定形状文件结合。最终结果显示了一张具有细节的南极洲地图。
摘要由CSDN通过智能技术生成

这里有一个如何实现你想要的东西的例子。我基本上遵循了Basemap example如何处理shapefiles并添加了一点shapely magic,以将轮廓限制在地图边界上。请注意,我第一次尝试从ax.patches中提取地图轮廓,但不知何故无效,因此我定义了一个半径为boundinglat的圆,并使用Basemap坐标转换功能对其进行了变换。在import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.basemap import Basemap

from matplotlib.collections import PatchCollection

from matplotlib.patches import Polygon

import shapely

from shapely.geometry import Polygon as sPolygon

boundinglat = -40

lats = np.arange(-90,boundinglat+1,1)

lons = np.arange(0,361,1)

X, Y = np.meshgrid(lons, lats)

data = np.random.rand(len(lats),len(lons))

fig, ax = plt.subplots(nrows=1, ncols=1, dpi=150)

m = Basemap(

ax = ax,

projection='spstere',boundinglat=boundinglat,lon_0=180,

resolution='i',round=True

)

xi, yi = m(X,Y)

cf = m.contourf(xi,yi,data)

#adjust the path to the shapefile here:

result = m.readshapefile(

'shapefiles/AntarcticaWGS84_contorno', 'antarctica',

zorder = 10, color = 'k', drawbounds = False)

#defining the outline of the map as shapely Polygon:

rim = [np.linspace(0,360,100),np.ones(100)*boundinglat,]

outline = sPolygon(np.asarray(m(rim[0],rim[1])).T)

#following Basemap tutorial for shapefiles

patches = []

for info, shape in zip(m.antarctica_info, m.antarctica):

#instead of a matplotlib Polygon, create first a shapely Polygon

poly = sPolygon(shape)

#check if the Polygon, or parts of it are inside the map:

if poly.intersects(outline):

#if yes, cut and insert

intersect = poly.intersection(outline)

verts = np.array(intersect.exterior.coords.xy)

patches.append(Polygon(verts.T, True))

ax.add_collection(PatchCollection(

patches, facecolor= 'w', edgecolor='k', linewidths=1., zorder=2

))

plt.show()

结果如下:

f4d2d1f66f4e1ab2b9041f6c60a9a388.png

希望这有帮助。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值