cartopy bounding box setting with set_extent broken for stereographic projections
Issue #420
new
Using the cartopy set_extent method with the bounding box coordiantes given as lat/lon tuples for the corners fails with seemingly random results. Typically, the left lower corner is ill-placed for Nothern Stereographic plots.
Workaround by manually computing the coodinates in projection coordinates and calling set_extent using those.
Example:
import os
import matplotlib.pyplot as plt
from netCDF4 import Dataset as netcdf_dataset
import numpy as np
from cartopy import config
import cartopy.crs as ccrs
# get the path of the file. It can be found in the repo data directory.
fname = os.path.join(config["repo_data_dir"],
'netcdf', 'HadISST1_SST_update.nc')
dataset = netcdf_dataset(fname)
sst = dataset.variables['sst'][0, :, :]
lats = dataset.variables['lat'][:]
lons = dataset.variables['lon'][:]
prj = ccrs.Stereographic(central_longitude=0, central_latitude=90)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection=prj)
print("default", ax.get_extent())
ax.set_extent([45, -135, 0, 0], ccrs.PlateCarree())
print("set1", ax.get_extent())
ax.contour(lons, lats, sst, 60, transform=ccrs.PlateCarree())
ax.coastlines()
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection=prj)
print("default", ax.get_extent())
coords = prj.transform_points(
ccrs.PlateCarree(), np.asarray([45, -135]), np.asarray([0, 0]))
ax.set_extent([coords[0, 0], coords[1, 0], coords[0, 1], coords[1, 1]], prj)
print("set2", ax.get_extent())
ax.contour(lons, lats, sst, 60, transform=ccrs.PlateCarree())
ax.coastlines()
plt.show()
Comments (1)
-
reporter - Log in to comment
See cartopy issue at https://github.com/SciTools/cartopy/issues/1331