You can run this notebook in a live session or view it on Github.
Working with Multidimensional Coordinates¶
Author: Ryan Abernathey
Many datasets have physical coordinates which differ from their logical coordinates. Xarray provides several ways to plot and analyze such datasets.
[1]:
%matplotlib inline
import numpy as np
import pandas as pd
import xarray as xr
import cartopy.crs as ccrs
from matplotlib import pyplot as plt
As an example, consider this dataset from the xarray-data repository.
[2]:
ds = xr.tutorial.open_dataset("rasm").load()
ds
---------------------------------------------------------------------------
PermissionError Traceback (most recent call last)
File /usr/lib/python3/dist-packages/pooch/utils.py:262, in make_local_storage(path, env)
258 if action == "create":
259 # When running in parallel, it's possible that multiple jobs will
260 # try to create the path at the same time. Use exist_ok to avoid
261 # raising an error.
--> 262 os.makedirs(path, exist_ok=True)
263 else:
File /usr/lib/python3.13/os.py:217, in makedirs(name, mode, exist_ok)
216 try:
--> 217 makedirs(head, exist_ok=exist_ok)
218 except FileExistsError:
219 # Defeats race condition when another thread created the path
File /usr/lib/python3.13/os.py:217, in makedirs(name, mode, exist_ok)
216 try:
--> 217 makedirs(head, exist_ok=exist_ok)
218 except FileExistsError:
219 # Defeats race condition when another thread created the path
File /usr/lib/python3.13/os.py:227, in makedirs(name, mode, exist_ok)
226 try:
--> 227 mkdir(name, mode)
228 except OSError:
229 # Cannot rely on checking for EEXIST, since the operating system
230 # could give priority to other errors like EACCES or EROFS
PermissionError: [Errno 13] Permission denied: '/sbuild-nonexistent'
The above exception was the direct cause of the following exception:
PermissionError Traceback (most recent call last)
Cell In[2], line 1
----> 1 ds = xr.tutorial.open_dataset("rasm").load()
2 ds
File /usr/lib/python3/dist-packages/xarray/tutorial.py:165, in open_dataset(name, cache, cache_dir, engine, **kws)
162 downloader = pooch.HTTPDownloader(headers=headers)
164 # retrieve the file
--> 165 filepath = pooch.retrieve(
166 url=url, known_hash=None, path=cache_dir, downloader=downloader
167 )
168 ds = _open_dataset(filepath, engine=engine, **kws)
169 if not cache:
File /usr/lib/python3/dist-packages/pooch/core.py:227, in retrieve(url, known_hash, fname, path, processor, downloader, progressbar)
222 action, verb = download_action(full_path, known_hash)
224 if action in ("download", "update"):
225 # We need to write data, so create the local data directory if it
226 # doesn't already exist.
--> 227 make_local_storage(path)
229 get_logger().info(
230 "%s data from '%s' to file '%s'.",
231 verb,
232 url,
233 str(full_path),
234 )
236 if downloader is None:
File /usr/lib/python3/dist-packages/pooch/utils.py:276, in make_local_storage(path, env)
272 if env is not None:
273 message.append(
274 f"Use environment variable '{env}' to specify a different location."
275 )
--> 276 raise PermissionError(" ".join(message)) from error
PermissionError: [Errno 13] Permission denied: '/sbuild-nonexistent' | Pooch could not create data cache folder '/sbuild-nonexistent/.cache/xarray_tutorial_data'. Will not be able to download data files.
In this example, the logical coordinates are x
and y
, while the physical coordinates are xc
and yc
, which represent the longitudes and latitudes of the data.
[3]:
print(ds.xc.attrs)
print(ds.yc.attrs)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[3], line 1
----> 1 print(ds.xc.attrs)
2 print(ds.yc.attrs)
NameError: name 'ds' is not defined
Plotting¶
Let’s examine these coordinate variables by plotting them.
[4]:
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(14, 4))
ds.xc.plot(ax=ax1)
ds.yc.plot(ax=ax2)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[4], line 2
1 fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(14, 4))
----> 2 ds.xc.plot(ax=ax1)
3 ds.yc.plot(ax=ax2)
NameError: name 'ds' is not defined

Note that the variables xc
(longitude) and yc
(latitude) are two-dimensional scalar fields.
If we try to plot the data variable Tair
, by default we get the logical coordinates.
[5]:
ds.Tair[0].plot()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[5], line 1
----> 1 ds.Tair[0].plot()
NameError: name 'ds' is not defined
In order to visualize the data on a conventional latitude-longitude grid, we can take advantage of xarray’s ability to apply cartopy map projections.
[6]:
plt.figure(figsize=(14, 6))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_global()
ds.Tair[0].plot.pcolormesh(
ax=ax, transform=ccrs.PlateCarree(), x="xc", y="yc", add_colorbar=False
)
ax.coastlines()
ax.set_ylim([0, 90]);
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[6], line 4
2 ax = plt.axes(projection=ccrs.PlateCarree())
3 ax.set_global()
----> 4 ds.Tair[0].plot.pcolormesh(
5 ax=ax, transform=ccrs.PlateCarree(), x="xc", y="yc", add_colorbar=False
6 )
7 ax.coastlines()
8 ax.set_ylim([0, 90]);
NameError: name 'ds' is not defined

Multidimensional Groupby¶
The above example allowed us to visualize the data on a regular latitude-longitude grid. But what if we want to do a calculation that involves grouping over one of these physical coordinates (rather than the logical coordinates), for example, calculating the mean temperature at each latitude. This can be achieved using xarray’s groupby
function, which accepts multidimensional variables. By default, groupby
will use every unique value in the variable, which is probably not what we want.
Instead, we can use the groupby_bins
function to specify the output coordinates of the group.
[7]:
# define two-degree wide latitude bins
lat_bins = np.arange(0, 91, 2)
# define a label for each bin corresponding to the central latitude
lat_center = np.arange(1, 90, 2)
# group according to those bins and take the mean
Tair_lat_mean = ds.Tair.groupby_bins("yc", lat_bins, labels=lat_center).mean(
dim=xr.ALL_DIMS
)
# plot the result
Tair_lat_mean.plot()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[7], line 6
4 lat_center = np.arange(1, 90, 2)
5 # group according to those bins and take the mean
----> 6 Tair_lat_mean = ds.Tair.groupby_bins("yc", lat_bins, labels=lat_center).mean(
7 dim=xr.ALL_DIMS
8 )
9 # plot the result
10 Tair_lat_mean.plot()
NameError: name 'ds' is not defined
The resulting coordinate for the groupby_bins
operation got the _bins
suffix appended: yc_bins
. This help us distinguish it from the original multidimensional variable yc
.
Note: This group-by-latitude approach does not take into account the finite-size geometry of grid cells. It simply bins each value according to the coordinates at the cell center. Xarray has no understanding of grid cells and their geometry. More precise geographic regridding for xarray data is available via the xesmf package.