html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue
https://github.com/pydata/xarray/issues/2009#issuecomment-375726695,https://api.github.com/repos/pydata/xarray/issues/2009,375726695,MDEyOklzc3VlQ29tbWVudDM3NTcyNjY5NQ==,6334793,2018-03-23T16:40:06Z,2018-03-23T16:43:45Z,NONE,"@mathause Thanks! Your solution worked brilliantly when used with contourf(). Here's how the code looks after implementing it.
```python
import cartopy.crs as ccrs
import xarray as xr
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
projection = False
ts = xr.tutorial.load_dataset('air_temperature').air[0, ...]
ncols, nrows = 2, 2
fig = plt.figure()
# ny,nx = ts.shape
# dx, dy = nx/ny, 1
# figsize = plt.figaspect(float(dy * ncols) / float(dx * nrows))
# fig = plt.figure(figsize=figsize)
gs = gridspec.GridSpec(ncols, nrows,wspace=0, hspace=0)
def set_map_layout(axes, width=17.0):
""""""
set figure height, given width
Needs to be called after all plotting is done.
Source: @mathause https://github.com/mathause/mplotutils/blob/v0.1.0/mplotutils/mpl_utils.py#L47-L100
Parameters
----------
axes : ndarray of (Geo)Axes
Array with all axes of the figure.
width : float
Width of the full figure in cm. Default 17
..note: currently only works if all the axes have the same aspect
ratio.
""""""
if isinstance(axes, plt.Axes):
ax = axes
else:
# assumes the first of the axes is representative for all
ax = axes.flat[0]
# read figure data
f = ax.get_figure()
bottom = f.subplotpars.bottom
top = f.subplotpars.top
left = f.subplotpars.left
right = f.subplotpars.right
hspace = f.subplotpars.hspace
wspace = f.subplotpars.wspace
# data ratio is the aspect
aspect = ax.get_data_ratio()
# get geometry tells how many subplots there are
nrow, ncol, __ = ax.get_geometry()
# width of one plot, taking into account
# left * wf, (1-right) * wf, ncol * wp, (1-ncol) * wp * wspace
wp = (width - width * (left + (1-right))) / (ncol + (ncol-1) * wspace)
# height of one plot
hp = wp * aspect
# height of figure
height = (hp * (nrow + ((nrow - 1) * hspace))) / (1. - (bottom + (1 - top)))
f.set_figwidth(width / 2.54)
f.set_figheight(height / 2.54)
for i in range(4):
if projection:
ax = plt.subplot(gs[i], projection=ccrs.PlateCarree())
ax.coastlines()
ts.plot.contourf(ax=ax, add_colorbar=False, add_labels=False, levels=11,
transform=ccrs.PlateCarree())
else:
ax = plt.subplot(gs[i])
ts.plot.contourf(ax=ax, add_colorbar=False, levels=11, add_labels=False)
ax.set_aspect('auto', adjustable='box-forced')
if (i == 0) or (i == 1):
ax.set_title('title')
if (i == 0) or (i == 2):
ax.set_ylabel('ylabel')
ax.set_xticks([])
ax.set_yticks([])
set_map_layout(ax)# plt.tight_layout()
# fig.subplots_adjust()
```
without projection:

With projection:

","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,307903558