python - 卡托皮县寄宿生
问题描述
您如何在 Cartopy 中绘制美国县界?
绘制州和国家边界非常简单
ax.add_feature(cfeature.BORDERS.with_scale('50m'))
ax.add_feature(cfeature.STATES.with_scale('50m'))
但我似乎找不到类似的方法来添加县界。这是 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()
这源自https://scitools.org.uk/cartopy/docs/v0.14/examples/feature_creation.html中的示例,该示例构造 a NaturalEarthFeature
,而不是 a ShapelyFeature
,但除此之外,原理几乎相同。
希望对你有用。
推荐阅读
- regex - Azure AD 配置正则表达式 | 指导
- python - Python中类对象内的for循环
- c++ - 连接不拾取变量更改
- java - 无法再从 Firebase 数据库获取数据?
- python - 属性错误:模块“提供者”没有属性“getDataFiles”
- java - Java 运行时的 exec() 方法未正确执行一个 shell 脚本,该脚本调用另一个带有重定向的 shell 脚本
- python - 以 BMP 格式保存 matplotlib 图
- python - 如何使用正则表达式删除与我需要的字符串在同一行的注释
- unity3d - 如何更改UI组超时透明?
- javascript - 在 React 中设置 Html 脚本位置