Правильное размещение цветовой шкалы относительно геоосей (картопия)
Используя Cartopy, я хотел бы иметь полный контроль над тем, куда идет моя цветовая панель. Обычно я делаю это, получая текущее положение осей в качестве основы, а затем создаю новые оси для цветовой шкалы. Это хорошо работает для стандартных осей matplotlib, но не при использовании Cartopy и geo_axes, потому что это искажает оси.
Итак, мой вопрос: как мне получить точное положение моих geo_axes?
Вот пример кода, основанный на документах Cartopyhttp://scitools.org.uk/cartopy/docs/latest/matplotlib/advanced_plotting.html:
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import os
from netCDF4 import Dataset as netcdf_dataset
from cartopy import config
def main():
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'][:]
#my preferred way of creating plots (even if it is only one plot)
ef, ax = plt.subplots(1,1,figsize=(10,5),subplot_kw={'projection': ccrs.PlateCarree()})
ef.subplots_adjust(hspace=0,wspace=0,top=0.925,left=0.1)
#get size and extent of axes:
axpos = ax.get_position()
pos_x = axpos.x0+axpos.width + 0.01# + 0.25*axpos.width
pos_y = axpos.y0
cax_width = 0.04
cax_height = axpos.height
#create new axes where the colorbar should go.
#it should be next to the original axes and have the same height!
pos_cax = ef.add_axes([pos_x,pos_y,cax_width,cax_height])
im = ax.contourf(lons, lats, sst, 60, transform=ccrs.PlateCarree())
ax.coastlines()
plt.colorbar(im, cax=pos_cax)
ax.coastlines(resolution='110m')
ax.gridlines()
ax.set_extent([-20, 60, 33, 63])
#when using this line the positioning of the colorbar is correct,
#but the image gets distorted.
#when omitting this line, the positioning of the colorbar is wrong,
#but the image is well represented (not distorted).
ax.set_aspect('auto', adjustable=None)
plt.savefig('sst_aspect.png')
plt.close()
if __name__ == '__main__': main()
Результирующий рисунок, при использовании «set_aspect»:
Результирующий рисунок, если опустить «set_aspect»:
По сути, я хотел бы получить первую фигуру (правильно расположенную цветную полосу), но без использования «set_aspect». Я думаю, что это должно быть возможно с некоторыми преобразованиями, но я не нашел решения до сих пор.
Спасибо!