6

我有两个 shapefile。一个是点特征 shapefile,名为“point.shp”,另一个是多边形 shapefile,名为“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()失败,为什么?

谢谢

4

1 回答 1

9

add_geometries 当前将几何图形转换为多边形,然后对其进行适当的着色,这当然意味着当您通过 add_geometries 点时,多边形是不可见的。未来,cartopy 可能会在这方面做得更好,但与此同时,听起来您只是想使用 scatter 之类的东西来可视化您的数据。

您可以通过从几何图形中获取 x 和 y 坐标值并将它们直接传递到 scatter 并使用适当的变换来实现此目的:

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()

输出

高温高压

于 2014-08-18T07:42:48.713 回答