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

Concepts

Importance

Notes

Xarray

Necessary

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

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
<xarray.Dataset> Size: 30kB
Dimensions:  (lat: 45, lon: 80)
Coordinates:
  * lat      (lat) int64 360B -90 -86 -82 -78 -74 -70 -66 ... 66 70 74 78 82 86
  * lon      (lon) float64 640B -180.0 -175.5 -171.0 ... 166.5 171.0 175.5
Data variables:
    psi      (lat, lon) float64 29kB ...

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
<xarray.UxDataset> Size: 43kB
Dimensions:  (n_face: 5400)
Dimensions without coordinates: n_face
Data variables:
    psi      (n_face) float64 43kB ...

Visualization

Xarray

xrds["psi"].plot(figsize=(12, 5), cmap="inferno")
<matplotlib.collections.QuadMesh at 0x7f7b6275f0a0>
../../_images/8fcb40903d7c189017df08a753dbf84cc42ed48b9dc9c08b09c0868195ebc067.png

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)