如何在Cartopy的地图上绘制填充的多边形

时间:2019-11-24 16:45:58

标签: matplotlib cartopy

已编辑,并从答案中添加了建议

我有一个经度/纬度的顶点列表,这些顶点定义了地图上多边形的角。我想使用cartopy在地图上绘制该多边形,其边缘是大圆。我尝试按照https://scitools.org.uk/cartopy/docs/v0.5/matplotlib/introductory_examples/02.polygon.html上的示例进行操作,但无法正常工作。到目前为止,这是我尝试过的:

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import matplotlib.patches as mpatches

map_proj = ccrs.Orthographic(central_latitude=0.0, central_longitude=80.0)
ax = plt.axes(projection=map_proj)

ax.set_global() # added following an answer to my question
ax.gridlines()

ax.coastlines(linewidth=0.5, color='k', resolution='50m')

lat_corners = np.array([-20.,  0., 50., 30.])
lon_corners = np.array([ 20., 90., 90., 30.]) + 15.0 # offset from gridline for clarity

poly_corners = np.zeros((len(lat_corners), 2), np.float64)
poly_corners[:,0] = lon_corners
poly_corners[:,1] = lat_corners

poly = mpatches.Polygon(poly_corners, closed=True, ec='r', fill=False, lw=1, fc=None, transform=ccrs.Geodetic())
ax.add_patch(poly)

Output image

请注意,这些线不是大圆,并且似乎有四个以上的顶点。我觉得这是一件很简单的事情,必须有一种方法,但我无法从Cartopy文档中找出来。

3 个答案:

答案 0 :(得分:1)

我认为这可能是因为Cartopy的默认变换分辨率对于此投影而言太低。您可以通过以下方法解决此问题:

<table class = "gfg" *ngFor="let product of products; let  a = index;"> 
  <tr> 
      <td class = "geeks">
          <div *ngIf="imagMap.get(product.id) == null">
              <img src="./assets/semimagem.png" class="imgTable img-fluid" alt="">
            </div>
        <img [src]="imagMap.get(product.id)" class="imgTable img-fluid">
      </td> 
  </tr> 
</table> 

这会产生漂亮的弯曲大圆弧。

答案 1 :(得分:0)

请记住,该示例使用了

ax.set_global()

答案 2 :(得分:0)

这是可运行的代码和输出。 积分归功于:问质者,ImportanceOfBeingErnest和ajdawson。

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import matplotlib.patches as mpatches

map_proj = ccrs.Orthographic(central_latitude=0.0, central_longitude=80.0)

map_proj._threshold /= 100.  # the default values is bad, users need to set them manually

ax = plt.axes(projection=map_proj)

ax.set_global() # added following an answer to my question
ax.gridlines()

ax.coastlines(linewidth=0.5, color='k', resolution='50m')

lat_corners = np.array([-20.,  0., 50., 30.])
lon_corners = np.array([ 20., 90., 90., 30.]) + 15.0 # offset from gridline for clarity

poly_corners = np.zeros((len(lat_corners), 2), np.float64)
poly_corners[:,0] = lon_corners
poly_corners[:,1] = lat_corners

poly = mpatches.Polygon(poly_corners, closed=True, ec='r', fill=True, lw=1, fc="yellow", transform=ccrs.Geodetic())
ax.add_patch(poly)

plt.show()

输出:

enter image description here