如何使用cartopy添加点要素shapefile以进行地图绘制

gep*_*cel 6 cartopy

我有两个shapefile.一个是点特征shapefile,名为"point.shp",另一个是名为"polygon.shp"的多边形shapefile.我想用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'))
Run Code Online (Sandbox Code Playgroud)

有没有人知道如何做到这一点,或为什么,没有检索所有点'x,y coords然后绘制它们?

并且坐标(x,y值),ax.plot()工作,但ax.scatter()失败,为什么?

谢谢

pel*_*son 8

add_geometries当前将几何转换为多边形,然后适当地对其进行着色,这当然意味着当您传递add_geometries点时,多边形是不可见的.在未来,潜在的折叠可以更好地做到这一点,但与此同时,听起来你只是想使用像分散这样的东西来可视化你的数据.

您可以通过从几何体中获取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()
Run Code Online (Sandbox Code Playgroud)

产量

HTH