您如何在 Cartopy 中绘制美国县边界?
绘制州和国家边界非常简单
ax.add_feature(cfeature.BORDERS.with_scale('50m'))
ax.add_feature(cfeature.STATES.with_scale('50m'))
Run Code Online (Sandbox Code Playgroud)
但我似乎找不到类似的方法来添加县界。这是 Basemap 的优点之一。
鉴于 cartopy 绘制 shapefile 的能力,这个问题基本上归结为“我在哪里可以找到美国县的轮廓?”。
在http://www.naturalearthdata.com/forums/topic/us-county-shape-file/的自然地球论坛上也提出了类似的问题。它指向http://nationalatlas.gov/mld/countyp.html上的一个位置,不幸的是,该位置出现了一些位腐烂问题。一个快速的谷歌建议现在可以在以下位置找到:
https://nationalmap.gov/small_scale/atlasftp.html?openChapters=chpbound#chpbound
我决定下载其中一个县 shapefile:
有了这个,我使用 cartopy 的 shapereader 来获取几何图形,并创建了一个自定义功能,然后可以将其添加到轴:
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import cartopy.io.shapereader as shpreader
import matplotlib.pyplot as plt
reader = shpreader.Reader('countyl010g.shp')
counties = list(reader.geometries())
COUNTIES = cfeature.ShapelyFeature(counties, ccrs.PlateCarree())
plt.figure(figsize=(10, 6))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(cfeature.LAND.with_scale('50m'))
ax.add_feature(cfeature.OCEAN.with_scale('50m'))
ax.add_feature(cfeature.LAKES.with_scale('50m'))
ax.add_feature(COUNTIES, facecolor='none', edgecolor='gray')
ax.coastlines('50m')
ax.set_extent([-83, -65, 33, 44])
plt.show()
Run Code Online (Sandbox Code Playgroud)
这源自https://scitools.org.uk/cartopy/docs/v0.14/examples/feature_creation.html上的示例,该示例构建了NaturalEarthFeature,而不是ShapelyFeature,但除此之外,原理几乎相同。
希望对你有用。
| 归档时间: |
|
| 查看次数: |
4638 次 |
| 最近记录: |