Working with Multidimensional Coordinates

    Many datasets have physical coordinates which differ from theirlogical coordinates. Xarray provides several ways to plot and analyzesuch datasets.

    As an example, consider this dataset from thexarray-data repository.

    1. In [7]: ds = xr.tutorial.open_dataset('rasm').load()
    2.  
    3. In [8]: ds
    4. Out[8]:
    5. <xarray.Dataset>
    6. Dimensions: (time: 36, x: 275, y: 205)
    7. Coordinates:
    8. * time (time) object 1980-09-16 12:00:00 ... 1983-08-17 00:00:00
    9. xc (y, x) float64 189.2 189.4 189.6 189.7 ... 17.65 17.4 17.15 16.91
    10. yc (y, x) float64 16.53 16.78 17.02 17.27 ... 28.26 28.01 27.76 27.51
    11. Dimensions without coordinates: x, y
    12. Tair (time, y, x) float64 nan nan nan nan nan ... 29.8 28.66 28.19 28.21
    13. Attributes:
    14. title: /workspace/jhamman/processed/R1002RBRxaaa01a/l...
    15. institution: U.W.
    16. source: RACM R1002RBRxaaa01a
    17. output_frequency: daily
    18. output_mode: averaged
    19. convention: CF-1.4
    20. references: Based on the initial model of Liang et al., 19...
    21. comment: Output from the Variable Infiltration Capacity...
    22. nco_openmp_thread_number: 1
    23. NCO: "4.6.0"
    24. history: Tue Dec 27 14:15:22 2016: ncatted -a dimension...

    Let’s examine these coordinate variables by plotting them.

    1.  
    2. In [12]: ds.xc.plot(ax=ax1);
    3.  
    4. In [13]: ds.yc.plot(ax=ax2);

    Note that the variables (longitude) and yc (latitude) aretwo-dimensional scalar fields.

    ../_images/xarray_multidimensional_coords_10_1.pngIn order to visualize the data on a conventional latitude-longitudegrid, we can take advantage of xarray’s ability to apply map projections.

    1. In [15]: plt.figure(figsize=(7,2));
    2.  
    3. In [16]: ax = plt.axes(projection=ccrs.PlateCarree());
    4.  
    5. In [17]: ds.Tair[0].plot.pcolormesh(ax=ax, transform=ccrs.PlateCarree(),
    6. ....: x='xc', y='yc', add_colorbar=False);
    7. ....:
    8.  
    9. In [18]: ax.coastlines();

    Multidimensional Groupby

    ../_images/xarray_multidimensional_coords_14_1.pngNote that the resulting coordinate for the groupby_bins operationgot the _bins suffix appended: . This help us distinguishit from the original multidimensional variable xc.