{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# plotting quantified data" ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "# to be able to read unit attributes following the CF conventions\n", "import cf_xarray.units # noqa: F401 # must be imported before pint_xarray\n", "import xarray as xr\n", "\n", "import pint_xarray # noqa: F401\n", "from pint_xarray import unit_registry as ureg\n", "\n", "xr.set_options(display_expand_data=False)" ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "## load the data" ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "ds = xr.tutorial.open_dataset(\"air_temperature\")\n", "data = ds.air\n", "data" ] }, { "cell_type": "markdown", "id": "4", "metadata": {}, "source": [ "## quantify the data" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "
\n", "Note: this example uses the data provided by the xarray.tutorial functions. As such, the units attributes follow the CF conventions, which pint does not understand by default. To still be able to read them we are using the registry provided by cf-xarray.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "quantified = data.pint.quantify()\n", "quantified" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "## work with the data" ] }, { "cell_type": "code", "execution_count": null, "id": "8", "metadata": {}, "outputs": [], "source": [ "monthly_means = quantified.pint.to(\"degC\").sel(time=\"2013\").groupby(\"time.month\").mean()\n", "monthly_means" ] }, { "cell_type": "markdown", "id": "9", "metadata": {}, "source": [ "Most operations will preserve the units but there are some which will drop them (see the [duck array integration status](https://xarray.pydata.org/en/stable/user-guide/duckarrays.html#missing-features) page). To work around that there are unit-aware versions on the `.pint` accessor. For example, to select data use `.pint.sel` instead of `.sel`:" ] }, { "cell_type": "code", "execution_count": null, "id": "10", "metadata": {}, "outputs": [], "source": [ "monthly_means.sel(\n", " lat=ureg.Quantity(4350, \"angular_minute\"),\n", " lon=ureg.Quantity(12000, \"angular_minute\"),\n", ")" ] }, { "cell_type": "markdown", "id": "11", "metadata": {}, "source": [ "## plot\n", "\n", "`xarray`'s plotting functions will cast the data to `numpy.ndarray`, so we need to \"dequantify\" first." ] }, { "cell_type": "code", "execution_count": null, "id": "12", "metadata": {}, "outputs": [], "source": [ "monthly_means.pint.dequantify(format=\"~P\").plot.imshow(col=\"month\", col_wrap=4)" ] } ], "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 5 }