Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib as mpl
- mpl.use('Agg')
- import numpy as np
- import cartopy as cart
- from matplotlib import pyplot as plt
- from matplotlib.colors import BoundaryNorm
- def plotmaps(precip_0, precip_1, precip_2, lats, lons):
- # fix lats and lons
- deltalat = np.mean(np.diff(lats)) / 2.
- deltalon = np.mean(np.diff(lons)) / 2.
- lats = lats - deltalat
- lons = lons - deltalon
- projection = cart.crs.PlateCarree()
- fig, axs = plt.subplots(nrows=1, ncols=3, figsize=(15, 10),
- constrained_layout=False,
- subplot_kw={'projection': projection})
- cmap = mpl.cm.get_cmap('rainbow')
- cmap.set_over('#ED0000')
- clevs = (0., 1., 2., 3., 4., 5., 10., 15., 20.,
- 25., 30., 40., 50., 60., 70., 80.)
- norm = BoundaryNorm(clevs, ncolors=cmap.N, clip=False)
- grid_limits = [-51., -27., -22., 5.]
- precips = [precip_0, precip_1, precip_2]
- axlist = axs.flatten()
- for i, axi in enumerate(axlist):
- img = axi.pcolormesh(lons, lats, precips[i], cmap=cmap,
- norm=norm, transform=projection)
- gf = axi.gridlines(xlocs=range(-180, 181, 4),
- ylocs=range(-90, 91, 2),
- draw_labels=True, linewidth=0.01)
- gf.xlabels_top = False
- gf.ylabels_right = False
- gf.xformatter = cart.mpl.gridliner.LONGITUDE_FORMATTER
- gf.yformatter = cart.mpl.gridliner.LATITUDE_FORMATTER
- gf.xlabel_style = {'color': 'k', 'size': 9, 'weight': 'bold'}
- gf.ylabel_style = {'color': 'k', 'size': 9, 'weight': 'bold'}
- states = cart.feature.NaturalEarthFeature(
- category='cultural',
- scale='50m', facecolor='none',
- name='admin_1_states_provinces_shp')
- axi.add_feature(states, edgecolor='k')
- countries = cart.feature.NaturalEarthFeature(
- category='cultural',
- scale='50m', facecolor='none',
- name='admin_0_countries')
- axi.add_feature(countries, edgecolor='k')
- axi.set_extent(grid_limits, projection)
- # pos x, pos y, size x, size y
- cb_ax = fig.add_axes([0.91, 0.302, 0.015, 0.383])
- bar = fig.colorbar(img, cax=cb_ax, extend='max',
- shrink=0.8, pad=0.0, spacing='uniform',
- orientation='vertical', ticks=clevs,
- extendfrac='auto')
- bar.set_label(label=f'(mm)', size=10, weight='bold')
- # bar.tick_params(labelsize=10) # not working
- # bar.img.set_yticklabels(clevs, fontsize=9, weight='bold') # not working
- axi.set_title('PRECIPITATION', fontsize=9, loc='left',
- weight='bold')
- # plt.show()
- plt.savefig('precips.png', dpi=200, bbox_inches='tight')
- plt.close()
Advertisement
Add Comment
Please, Sign In to add comment