iris icon indicating copy to clipboard operation
iris copied to clipboard

Add a Zonal Mean gallery example

Open trexfeathers opened this issue 2 years ago • 0 comments

This came out of supporting a user at the UK Met Office. We were able to produce code that aligns a linear plot with one of the axes of a cartographic plot - ideal for zonal means - based on this Stack Overflow post.

An example plot & script is below, although the data loading steps have been removed. We should be able to adapt this to work with something from iris-sample-data. A previous attempt stalled due to lack of contributor time - #4871 - so that could be used as a template.


Zonal mean plot example

sal_ano

from cartopy.crs import PlateCarree
import iris
from iris.analysis import MEAN
import iris.plot as iplt
import matplotlib.pyplot as plt
import matplotlib.cm as mpl_cm
from mpl_toolkits.axes_grid1 import make_axes_locatable
import seaborn as sns

sns.set(style='whitegrid')
cmap_BrBG_r = mpl_cm.get_cmap('BrBG_r')

fig = plt.figure(figsize=[12, 4])
ax1 = fig.add_subplot(111, projection=PlateCarree())
ax1.set_extent([-90, 0, -10, 70], crs=ccrs.PlateCarree())
plt.sca(ax1)
im=ax1.contourf(lon_n216,lat_n216,tmptmp,levels=levels,cmap=cmap_BrBG_r,extend='both')
ax1.coastlines()
ax1.gridlines()
ax1.set_xticks([-100, -80, -60, -40, -20, 0], crs=ccrs.PlateCarree())
ax1.set_yticks([-10, 0, 10, 20, 30, 40, 50 , 60 , 70], crs=ccrs.PlateCarree())
ax1.set_title('Upper ocean salinity anomaly')
ax1.set_ylabel('latitude')
ax1.set_xlabel('longitude')

divider = make_axes_locatable(ax1)

ax2 = divider.new_vertical(size="5%", pad=0.5, axes_class=plt.Axes, pack_start=True)
fig.add_axes(ax2)
plt.sca(ax2)
cbar=plt.colorbar(im, cax=ax2,orientation='horizontal')
cbar.ax.set_xlabel('salinity [PSU]')

ax3 = divider.new_horizontal(size="30%", pad=0.2, axes_class=plt.Axes)
fig.add_axes(ax3)
plt.sca(ax3)
ax3.plot(avg_sa_arr[0,:],lat_n216,color='k')
ax3.axvline(0,color='k',linewidth=0.5)
ax3.set_ylim(-10,70)
ax3.set_title('Zonal mean')
ax3.set_ylabel('latitude')
ax3.set_xlabel('salinity [PSU]')
ax3.yaxis.set_label_position("right")
ax3.yaxis.tick_right()

outfile='sal_ano.png'
plt.savefig(outfile,bbox_inches='tight', pad_inches=0.1, dpi=300)
plt.close()

trexfeathers avatar Aug 09 '22 10:08 trexfeathers