marcelorodriguesss

Subplot Cartopy

Oct 22nd, 2019
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. import matplotlib as mpl
  2. mpl.use('Agg')
  3. import numpy as np
  4. import cartopy as cart
  5. from matplotlib import pyplot as plt
  6. from matplotlib.colors import BoundaryNorm
  7.  
  8. def plotmaps(precip_0, precip_1, precip_2, lats, lons):
  9.  
  10.     # fix lats and lons
  11.     deltalat = np.mean(np.diff(lats)) / 2.
  12.     deltalon = np.mean(np.diff(lons)) / 2.
  13.     lats = lats - deltalat
  14.     lons = lons - deltalon
  15.  
  16.     projection = cart.crs.PlateCarree()
  17.  
  18.     fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(15, 10),
  19.                             constrained_layout=False,
  20.                             subplot_kw={'projection': projection})
  21.  
  22.     cmap = mpl.cm.get_cmap('rainbow')
  23.  
  24.     cmap.set_over('#ED0000')
  25.  
  26.     clevs = (0., 1., 2., 3., 4., 5., 10., 15., 20.,
  27.              25., 30., 40., 50., 60., 70., 80.)
  28.  
  29.     norm = BoundaryNorm(clevs, ncolors=cmap.N, clip=False)
  30.  
  31.     grid_limits = [-51., -27., -22., 5.]
  32.  
  33.     precips = [precip_0, precip_1, precip_2]
  34.  
  35.     axlist = axs.flatten()
  36.  
  37.     for i, axi in enumerate(axlist):
  38.  
  39.         img = axi.pcolormesh(lons, lats, precips[i], cmap=cmap,
  40.                              norm=norm, transform=projection)
  41.  
  42.         gf = axi.gridlines(xlocs=range(-180, 181, 4),
  43.                            ylocs=range(-90, 91, 2),
  44.                            draw_labels=True, linewidth=0.01)
  45.  
  46.         gf.xlabels_top = False
  47.         gf.ylabels_right = False
  48.         gf.xformatter = cart.mpl.gridliner.LONGITUDE_FORMATTER
  49.         gf.yformatter = cart.mpl.gridliner.LATITUDE_FORMATTER
  50.         gf.xlabel_style = {'color': 'k', 'size': 9, 'weight': 'bold'}
  51.         gf.ylabel_style = {'color': 'k', 'size': 9, 'weight': 'bold'}
  52.  
  53.         states = cart.feature.NaturalEarthFeature(
  54.             category='cultural',
  55.             scale='50m', facecolor='none',
  56.             name='admin_1_states_provinces_shp')
  57.  
  58.         axi.add_feature(states, edgecolor='k')
  59.  
  60.         countries = cart.feature.NaturalEarthFeature(
  61.             category='cultural',
  62.             scale='50m', facecolor='none',
  63.             name='admin_0_countries')
  64.  
  65.         axi.add_feature(countries, edgecolor='k')
  66.  
  67.         axi.set_extent(grid_limits, projection)
  68.  
  69.     # pos x,  pos y, size x, size y
  70.     cb_ax = fig.add_axes([0.91, 0.302, 0.015, 0.383])
  71.  
  72.     bar = fig.colorbar(img, cax=cb_ax, extend='max',
  73.                        shrink=0.8, pad=0.0, spacing='uniform',
  74.                        orientation='vertical', ticks=clevs,
  75.                        extendfrac='auto')
  76.  
  77.     bar.set_label(label=f'(mm)', size=10, weight='bold')
  78.  
  79.     # bar.tick_params(labelsize=10)  # not working
  80.  
  81.     # bar.img.set_yticklabels(clevs, fontsize=9, weight='bold')  # not working
  82.  
  83.     axi.set_title('PRECIPITATION', fontsize=9, loc='left',
  84.                   weight='bold')
  85.  
  86.     # plt.show()
  87.  
  88.     plt.savefig('precips.png', dpi=200, bbox_inches='tight')
  89.  
  90.     plt.close()
Advertisement
Add Comment
Please, Sign In to add comment