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.

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

Loading...

UXarray

---------------------------------------------------------------------------
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.14/site-packages/uxarray/core/api.py:163, in open_dataset(grid_filename_or_obj, filename_or_obj, latlon, use_dual, grid_kwargs, **kwargs)
    160 # map each dimension to its UGRID equivalent
    161 ds = _map_dims_to_ugrid(ds, uxgrid._source_dims_dict)
--> 163 uxds = UxDataset(ds, uxgrid=uxgrid, source_datasets=str(filename_or_obj))
    165 return uxds

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

File ~/micromamba/envs/unstructured-grid-viz-cookbook-dev/lib/python3.14/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

UXarray

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