【问题标题】:cartopy shapefile turning out as a dotcartopy shapefile 变成了一个点
【发布时间】:2022-01-02 15:37:00
【问题描述】:

我正在尝试从底图切换到 cartopy,但在绘制简单的 shapefile 时遇到了一些困难。

使用底图我可以得到这个

代码:

from mpl_toolkits.basemap import Basemap
# define map extent
lllat=1.1497
urlat=1.5133
lllon=103.5822
urlon=104.1579

# Set up Basemap instance
m = Basemap(
    projection = 'merc',
    llcrnrlon = lllon, llcrnrlat = lllat, urcrnrlon = urlon, urcrnrlat = urlat,
    resolution='h')

shp_info = m.readshapefile('singapore_shapefile',
                            'singapore',
                            drawbounds=True)

但是使用 cartopy,我得到一个中间有一个点的空白。

import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
from cartopy.feature import ShapelyFeature

# define map extent
lllat=1.1497
urlat=1.5133
lllon=103.5822
urlon=104.1579

# Set up cartopy instance
m = ccrs.Mercator()
ax = plt.axes(projection=m)
plt.gcf().set_size_inches(20, 10)

reader = shpreader.Reader("singapore_shapefile")
shape_feature = ShapelyFeature(reader.geometries(), m, facecolor="w", 
                               edgecolor='black', lw=1)
ax.add_feature(shape_feature)

plt.savefig("test.png")

谁能指出我正确的方向?

形状文件:https://drive.google.com/file/d/1suB-VD65bmwesJo01mAvVUolRReWUkdV/view?usp=sharing

【问题讨论】:

    标签: python matplotlib-basemap cartopy


    【解决方案1】:

    需要注意两点:

    1. 您需要设置地图范围,否则,感兴趣的区域对于整个世界来说太小了。
    2. ShapelyFeature 函数需要 shapefile 的投影而不是地图。

    下面是修改后的代码。

    import cartopy.crs as ccrs
    import cartopy.io.shapereader as shpreader
    from cartopy.feature import ShapelyFeature
    import matplotlib.pyplot as plt
    
    # define map extent
    lllat=1.1497
    urlat=1.5133
    lllon=103.5822
    urlon=104.1579
    
    # Set up cartopy instance
    m = ccrs.Mercator()
    ax = plt.axes(projection=m)
    
    plt.gcf().set_size_inches(20, 10)
    
    
    extent=[lllon,urlon,lllat,urlat]
    ax.set_extent(extent)
    
    reader = shpreader.Reader("singapore_shapefile")
    shape_feature = ShapelyFeature(reader.geometries(), ccrs.PlateCarree(), facecolor="w",
                                   edgecolor='black', lw=1)
    ax.add_feature(shape_feature)
    
    plt.savefig("singapore.png")
    

    这是输出图。

    【讨论】:

      猜你喜欢
      • 2017-02-17
      • 1970-01-01
      • 2014-10-10
      • 2017-04-24
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-09
      相关资源
      最近更新 更多