Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

UXarray logo

Comparison to Xarray

In this tutorial, you’ll learn about:

  • The differences and similarities between UXarray’s and Xarray’s plotting routines

  • Using hvPlot with Xarray

Prerequisites

ConceptsImportanceNotes
XarrayNecessary

Time to learn: 5 minutes


Introduction

For users coming from an Xarray background, much of UXarray’s design is familiar. This notebook showcases an example of transitioning a visualization of a structured grid using Xarray into a visualization of an unstructured grid using UXarray.

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import uxarray as ux
import xarray as xr
Loading...
Loading...
Loading...
Loading...

Data

We use two variations of the outCSne30 grid for this example. One of them is the original unstructured cube sphere, with the other being a remapped structured version.

Xarray

base_path = "../../meshfiles/"
ds_path = base_path + "outCSne30.structured.nc"
xrds = xr.open_dataset(ds_path)
xrds
Loading...

UXarray

base_path = "../../meshfiles/"
grid_filename = base_path + "outCSne30.grid.ug"
data_filename = base_path + "outCSne30.data.nc"

uxds = ux.open_dataset(grid_filename, data_filename)
uxds
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[3], line 5
      1 base_path = "../../meshfiles/"
      2 grid_filename = base_path + "outCSne30.grid.ug"
      3 data_filename = base_path + "outCSne30.data.nc"
      4 
----> 5 uxds = ux.open_dataset(grid_filename, data_filename)
      6 uxds

File ~/micromamba/envs/unstructured-grid-viz-cookbook-dev/lib/python3.14t/site-packages/uxarray/core/api.py:194, in open_dataset(grid_filename_or_obj, filename_or_obj, latlon, use_dual, grid_kwargs, **kwargs)
    191 # map each dimension to its UGRID equivalent
    192 ds = _map_dims_to_ugrid(ds, uxgrid._source_dims_dict, uxgrid)
--> 194 uxds = UxDataset(ds, uxgrid=uxgrid, source_datasets=str(filename_or_obj))
    196 return uxds

File ~/micromamba/envs/unstructured-grid-viz-cookbook-dev/lib/python3.14t/site-packages/uxarray/core/dataset.py:82, in UxDataset.__init__(self, uxgrid, source_datasets, *args, **kwargs)
     79 else:
     80     self._uxgrid = uxgrid
---> 82 super().__init__(*args, **kwargs)

File ~/micromamba/envs/unstructured-grid-viz-cookbook-dev/lib/python3.14t/site-packages/xarray/core/dataset.py:389, in Dataset.__init__(self, data_vars, coords, attrs)
    385     ) -> None:
    386         if data_vars is None:
    387             data_vars = {}
    388         if isinstance(data_vars, Dataset):
--> 389             raise TypeError(
    390                 "Passing a Dataset as `data_vars` to the Dataset constructor is"
    391                 " not supported. Use `ds.copy()` to create a copy of a Dataset."
    392             )

TypeError: Passing a Dataset as `data_vars` to the Dataset constructor is not supported. Use `ds.copy()` to create a copy of a Dataset.

Visualization

Xarray

xrds["psi"].plot(figsize=(12, 5), cmap="inferno");

UXarray

uxds["psi"].plot(width=800, height=400, backend="matplotlib", cmap="inferno")

Using hvPlot to combine UXarray & Xarray Plots

Since UXarray is written using hvPlot, we can visualize Xarray and UXarray plots together by using hvplot.xarray.

See also:

To learn more about hvPlot and Xarray, please refer to the hvPlot Documentation
import holoviews as hv
import hvplot.xarray

hv.extension("bokeh")
(
    xrds.hvplot(cmap="inferno", title="Xarray with hvPlot", width=800, height=400)
    + uxds["psi"].plot(
        cmap="inferno",
        title="UXarray Plot",
        width=800,
        height=400,
        periodic_elements="split",
    )
).cols(1)