我有两个形状文件。一个是点要素形状文件,名为“point.shp”,另一个是名为“polygon.shp”的多边形形状文件。我想使用 cartopy 添加到地图中。
我设法添加“polygon.shp”,但添加“point.shp”失败。
这是我的代码:
import matplotlib.pyplot as plt
from cartopy import crs
from cartopy.io.shapereader import Reader
from cartopy.feature import ShapelyFeature
ax = plt.axes(projection=crs.PlateCarree())
# add the polygon file, worked
ax.add_geometries(Reader("polygon.shp").geometries(), crs.PlateCarree(), facecolor='w')
# or(also worked):
ax.add_feature(ShapelyFeature(Reader("polygon.shp").geometries(), crs.PlateCarree(), facecolor='r'))
# but these two ways both failed with the "point.shp"
ax.add_geometries(Reader("point.shp").geometries(), crs.PlateCarree())
# or, this doesn't work neither:
ax.add_feature(ShapelyFeature(Reader("polygon.shp").geometries(), crs.PlateCarree(), facecolor='r'))
有谁知道如何做到这一点,或者为什么不检索所有点的 x, y 坐标然后绘制它们?
并使用坐标(x,y 值),ax.plot()
有效,但是ax.scatter()
失败了,为什么?
Thanks
add_geometries 当前将几何图形转换为多边形,然后对其进行适当的着色,这当然意味着当您传递点时 add_geometries 时,多边形不可见。未来,cartopy 可能会在这方面做得更好,但与此同时,听起来您只想使用 scatter 之类的东西来可视化您的数据。
您可以通过从几何图形中获取 x 和 y 坐标值并将这些值直接传递到使用适当的变换进行分散来实现此目的:
import cartopy.crs as ccrs
import cartopy.io
import matplotlib.pyplot as plt
fname = cartopy.io.shapereader.natural_earth(resolution='10m',
category='cultural',
name='populated_places_simple')
plt.figure(figsize=(12, 6))
ax = plt.axes(projection=ccrs.Robinson())
ax.set_title('Populated places of the world.')
ax.coastlines()
points = list(cartopy.io.shapereader.Reader(fname).geometries())
ax.scatter([point.x for point in points],
[point.y for point in points],
transform=ccrs.Geodetic())
plt.show()
HTH
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)