{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Plotting Quantified Data" ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "from astropy import units as u\n", "\n", "import astropy_xarray # noqa: F401\n", "\n", "# to be able to read unit attributes following the CF conventions\n", "# import cf_xarray.units # must be imported before astropy_xarray\n", "u.set_enabled_aliases(\n", " {\n", " \"degK\": u.Kelvin,\n", " \"K\": u.Kelvin,\n", " \"degrees_north\": u.deg,\n", " \"degrees_east\": u.deg,\n", " },\n", ")\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 astropy does not understand by default. To still be able to read them, registry be aliases can be used. For more information, see cf-xarray.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "quantified = data.astropy.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.astropy.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 `.astropy` accessor. For example, to select data use `.astropy.sel` instead of `.sel`:" ] }, { "cell_type": "code", "execution_count": null, "id": "10", "metadata": {}, "outputs": [], "source": [ "monthly_means.sel(\n", " lat=u.Quantity(4350, \"arcmin\"),\n", " lon=u.Quantity(12000, \"arcmin\"),\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.astropy.dequantify(format=\"unicode\").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 }