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.
In [1]: import numpy as np
In [2]: import pandas as pd
In [3]: import xarray as xr
In [4]: import netCDF4
In [5]: import cartopy.crs as ccrs
In [6]: import matplotlib.pyplot as plt
As an example, consider this dataset from the xarray-data repository.
In [7]: ds = xr.tutorial.load_dataset('rasm')
OSErrorTraceback (most recent call last)
<ipython-input-7-7588dce177cb> in <module>()
----> 1 ds = xr.tutorial.load_dataset('rasm')
/build/python-xarray-pj47QL/python-xarray-0.10.7/xarray/tutorial.py in load_dataset(name, cache, cache_dir, github_url, branch, **kws)
61 # May want to add an option to remove it.
62 if not _os.path.isdir(longdir):
---> 63 _os.mkdir(longdir)
64
65 url = '/'.join((github_url, 'raw', branch, fullname))
OSError: [Errno 2] No such file or directory: '/sbuild-nonexistent/.xarray_tutorial_data'
In [8]: ds
Out[8]:
<xarray.Dataset>
Dimensions: (time: 3, x: 2, y: 2)
Coordinates:
lat (x, y) float64 42.25 42.21 42.63 42.59
lon (x, y) float64 -99.83 -99.32 -99.79 -99.23
* time (time) datetime64[ns] 2014-09-06 2014-09-07 2014-09-08
reference_time datetime64[ns] 2014-09-05
day (time) int64 6 7 8
Dimensions without coordinates: x, y
Data variables:
temperature (x, y, time) float64 11.04 23.57 20.77 9.346 6.683 17.17 ...
precipitation (x, y, time) float64 5.904 2.453 3.404 9.847 9.195 ...
In this example, the logical coordinates are x
and y
, while
the physical coordinates are xc
and yc
, which represent the
latitudes and longitude of the data.
In [9]: ds.xc.attrs
AttributeErrorTraceback (most recent call last)
<ipython-input-9-58b1081550ce> in <module>()
----> 1 ds.xc.attrs
/build/python-xarray-pj47QL/python-xarray-0.10.7/xarray/core/common.py in __getattr__(self, name)
174 return source[name]
175 raise AttributeError("%r object has no attribute %r" %
--> 176 (type(self).__name__, name))
177
178 def __setattr__(self, name, value):
AttributeError: 'Dataset' object has no attribute 'xc'
In [10]: ds.yc.attrs
AttributeErrorTraceback (most recent call last)
<ipython-input-10-f2e4ac0aa2f2> in <module>()
----> 1 ds.yc.attrs
/build/python-xarray-pj47QL/python-xarray-0.10.7/xarray/core/common.py in __getattr__(self, name)
174 return source[name]
175 raise AttributeError("%r object has no attribute %r" %
--> 176 (type(self).__name__, name))
177
178 def __setattr__(self, name, value):
AttributeError: 'Dataset' object has no attribute 'yc'
Plotting¶
Let’s examine these coordinate variables by plotting them.
In [11]: fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(9,3))
In [12]: ds.xc.plot(ax=ax1);
In [13]: ds.yc.plot(ax=ax2);

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.
In [14]: ds.Tair[0].plot();

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.
In [15]: plt.figure(figsize=(7,2));
In [16]: ax = plt.axes(projection=ccrs.PlateCarree());
In [17]: ds.Tair[0].plot.pcolormesh(ax=ax, transform=ccrs.PlateCarree(),
....: x='xc', y='yc', add_colorbar=False);
....:
In [18]: ax.coastlines();
In [19]: plt.tight_layout();

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.
# define two-degree wide latitude bins
In [20]: lat_bins = np.arange(0, 91, 2)
# define a label for each bin corresponding to the central latitude
In [21]: lat_center = np.arange(1, 90, 2)
# group according to those bins and take the mean
In [22]: Tair_lat_mean = ds.Tair.groupby_bins('xc', lat_bins, labels=lat_center).mean()
AttributeErrorTraceback (most recent call last)
<ipython-input-22-d2cf517792bc> in <module>()
----> 1 Tair_lat_mean = ds.Tair.groupby_bins('xc', lat_bins, labels=lat_center).mean()
/build/python-xarray-pj47QL/python-xarray-0.10.7/xarray/core/common.py in __getattr__(self, name)
174 return source[name]
175 raise AttributeError("%r object has no attribute %r" %
--> 176 (type(self).__name__, name))
177
178 def __setattr__(self, name, value):
AttributeError: 'Dataset' object has no attribute 'Tair'
# plot the result
In [23]: Tair_lat_mean.plot();

Note that the resulting coordinate for the groupby_bins
operation
got the _bins
suffix appended: xc_bins
. This help us distinguish
it from the original multidimensional variable xc
.