我如何在特殊投影中的Cartopy底图上绘制png图像?

问题描述 投票:0回答:1

我正在尝试打开png图像,以将该图像绘制在Cartopy的底图上。我已经按照以下说明进行操作:https://scitools.org.uk/cartopy/docs/v0.15/examples/geostationary.html


import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy
from PIL import Image



def create_map(image):

    res = '10m'
    proj = ccrs.NorthPolarStereo(central_longitude=10.0)
    img = plt.imread(image)
    img_extent = (2.0715, 15.72, 46.9526, 54.5877)

    ax = plt.axes(projection = proj)
    ax.set_extent = ([3.0889, 17.1128, 46.1827, 55.5482])    

    land_10m = cfeature.NaturalEarthFeature('physical', 'land', res,
                                         edgecolor = 'face', 
                                         facecolor=cfeature.COLORS['land'],
                                         zorder=0)

    state_provinces_10m = cfeature.NaturalEarthFeature(category = 'cultural', 
                                                       name = 'admin_1_states_provinces_lines',
                                                       scale = res,
                                                       facecolor = none)

    ax.add_feature(state_provinces_10m, edgecolor='gray')
    ax.add_feature(land_10m)
    ax.add_feature(cartopy.feature.BORDERS.with_scale(res), linestyle='-', linewith=1)
    ax.add_feature(cartopy.feature.COASTLINE.with_scale(res), linestyle='-')

    plt.imshow(img, origin='upper', extent=img_extent, transform = proj)

    plt.show()

create_map('image.png')

我的结果是定义范围的底图,但没有我的图像。我做错了吗?

问候

matplotlib python-3.7 cartopy
1个回答
0
投票

您对imshow的转换参数几乎可以肯定是不正确的。北极立体投影中的(2.0715, 15.72, 46.9526, 54.5877)图像范围是靠近北极的很小区域,不在所需的地图范围内。从上下文看来,范围是在地理坐标中指定的,在这种情况下,解决方案应该是在transform=ccrs.PlateCarree()调用中使用imshow

一般而言,我建议您始终清楚自己的坐标系,所以我建议

def create_map(image):

    res = '10m'
    proj = ccrs.NorthPolarStereo(central_longitude=10.0)
    img = plt.imread(image)
    img_extent = (2.0715, 15.72, 46.9526, 54.5877)

    ax = plt.axes(projection = proj)
    # EXPLICIT CRS HERE:
    ax.set_extent([3.0889, 17.1128, 46.1827, 55.5482], crs=ccrs.PlateCarree())    

    land_10m = cfeature.NaturalEarthFeature('physical', 'land', res,
                                            edgecolor = 'face', 
                                            facecolor=cfeature.COLORS['land'],
                                            zorder=0)

    state_provinces_10m = cfeature.NaturalEarthFeature(category = 'cultural', 
                                                       name = 'admin_1_states_provinces_lines',
                                                       scale = res,
                                                       facecolor = none)

    ax.add_feature(state_provinces_10m, edgecolor='gray')
    ax.add_feature(land_10m)
    ax.add_feature(cartopy.feature.BORDERS.with_scale(res), linestyle='-', linewith=1)
    ax.add_feature(cartopy.feature.COASTLINE.with_scale(res), linestyle='-')

    # USE CORRECT CRS HERE
    plt.imshow(img, origin='upper', extent=img_extent, transform=ccrs.PlateCarree())

    plt.show()

此文档提供有关Cartopy中变换/投影的指南:https://scitools.org.uk/cartopy/docs/latest/tutorials/understanding_transform.html

© www.soinside.com 2019 - 2024. All rights reserved.