Latitude Binning

This example shows how to plot ozone at 32.5 km for weekly means grouped into 5 degree latitude bins.

import os
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt

# define a function to do the latitude binning with xarray's map function
def latitude_bin(ds, lat_res, lat_min, lat_max):
    lat_bins = np.arange(lat_min, lat_max, lat_res)
    data_groups = ds.groupby_bins(ds.latitude, lat_bins, labels=lat_bins[1::] - lat_res / 2)
    data_mean = data_groups.mean(dim='time')
    return data_mean

# load the 2012 data and set time as the dimension
v7_folder = r'path\to\v7_data'
data = xr.open_mfdataset(os.path.join(v7_folder, '*.nc'))
data = data.swap_dims({'profile_id': 'time'})
data = data.sel(time='2012')

# resample for weekly means, loffset fixes the alignment of ticks when plotting
data_resample = data.resample(time='7D', loffset='84H')
data_bin = data_resample.map(latitude_bin, args=(5, -90, 91))

data_bin.ozone_concentration.sel(altitude=32.5).plot(x='time', figsize=[8, 5])
../_images/o3_lat_bins.png