Woodz
Woodz

Reputation: 191

How can i plot a png image on a cartopy basemap in a special projection?

I'm trying to open a png-Image to plot this image on a basemap of cartopy. I already followed these instructions on: 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')

My results are a basemap of the defined extent but without my image. What i am doing wrong?

regards

Upvotes: 0

Views: 2887

Answers (1)

ajdawson
ajdawson

Reputation: 3333

Your transform argument for imshow is almost certainly incorrect. An image extent of (2.0715, 15.72, 46.9526, 54.5877) in North polar stereographic projection is a very small region near the North Pole, which is not within your desired map extent. From context it looks like the extent is specified in geographic coordinates, in which case the solution should be to use transform=ccrs.PlateCarree() in your imshow call.

In general I recommend being explicit about what your coordinate system is at all times, so I would suggest

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

This documentation provides guidance on transforms/projections in Cartopy: https://scitools.org.uk/cartopy/docs/latest/tutorials/understanding_transform.html

Upvotes: 2

Related Questions