title: Cartopy background color (outside of data domain)
body: <p>In a Cartopy map, I would like to have the region not covered by any data (outside of my domain) colored in e.g. lightgrey.
  Have played with <code>background_patch</code> and looked at this example <a href="https://stackoverflow.com/questions/32200438/change-the-background-colour-of-a-projected-matplotlib-axis">Change the background colour of a projected Matplotlib axis</a> but still can't figure out to do what I want.</p>
  
  <p>Here is an artificial example, where I make the domain bounds visible by a red line. Instead I would like to have the area outside of the red line colored in lightgrey.</p>
  
  <p>Many thanks!</p>
  
  <p><strong>EDIT:</strong> 
  Changed the projection to LambertConformal to demonstrate that the solution proposed below (<a href="https://stackoverflow.com/questions/34898312/cartopy-background-color-outside-of-data-domain/34989375#34989375">Cartopy background color (outside of data domain)</a>) does only work for rectangular grids. See additional figures below.
  <a href="https://i.stack.imgur.com/bjVJH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bjVJH.png" alt="The domain bounds drawn"></a>
  <a href="https://i.stack.imgur.com/7Aftw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7Aftw.png" alt="The proposed solution"></a></p>
  
  <pre><code>import matplotlib.pyplot as plt
  import matplotlib as mpl
  import cartopy.crs as ccrs
  from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
  import numpy as np
  
  #create some lons/lats
  lats = np.linspace(20,40,50)
  lons = np.linspace(110,130,50)
  lons,lats = np.meshgrid(lons,lats)
  
  #some data
  thedata = np.zeros_like(lats)
  #some 'cloud' in the data
  thedata[5:8,7:13] = 1
  
  #theproj = ccrs.Mercator()
  theproj = ccrs.LambertConformal() #choose another projection to obtain non-rectangular grid
  ef, axar = plt.subplots(1,1, subplot_kw={'projection': theproj})#, 'axisbg': 'w'
  ef.subplots_adjust(hspace=0.,wspace=0.,bottom=0.05,top=0.95,left=0.03,right=0.98)    
  axar.coastlines()
  
  mycmap = mpl.colors.ListedColormap(['white', 'black'])
  bounds=[0,0.5,1]
  norm = mpl.colors.BoundaryNorm(bounds, mycmap.N)
  
  im = axar.pcolormesh(lons,lats,thedata,cmap=mycmap, transform=ccrs.PlateCarree())
  im.set_norm(norm)
  
  #make the extent larger to see a margin outside of the domain
  axar.set_extent([lons[0,0]-1,lons[-1,-1]+1,lats[0,0]-1,lats[-1,-1]+1])
  
  
  #for illustration: make the domain bounds visible
  #but instead of the red domain bounds I would like to have the background (outside of the domain) in some color (lightgrey)
  axar.plot(lons[:,0],lats[:,0],'r', transform=ccrs.PlateCarree())
  axar.plot(lons[:,-1],lats[:,0],'r', transform=ccrs.PlateCarree())
  axar.plot(lons[0,:],lats[0,:],'r', transform=ccrs.PlateCarree())
  axar.plot(lons[0,:],lats[-1,:],'r', transform=ccrs.PlateCarree())
  
  #some decoration to see where we are
  gl = axar.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,
                    linewidth=2, color='gray', alpha=0.5, linestyle='--')
  gl.xformatter = LONGITUDE_FORMATTER
  gl.yformatter = LATITUDE_FORMATTER    
  
  plt.show()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/W1HS9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/W1HS9.png" alt="enter image description here"></a></p>