|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "round-optimization", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# plotting quantified data" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": null, |
| 14 | + "id": "greatest-smart", |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "import xarray as xr\n", |
| 19 | + "import pint_xarray" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "markdown", |
| 24 | + "id": "fuzzy-maintenance", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "## load the data" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": null, |
| 33 | + "id": "proved-racing", |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [], |
| 36 | + "source": [ |
| 37 | + "ds = xr.tutorial.open_dataset(\"air_temperature\")\n", |
| 38 | + "data = ds.air\n", |
| 39 | + "data" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "markdown", |
| 44 | + "id": "medium-backup", |
| 45 | + "metadata": {}, |
| 46 | + "source": [ |
| 47 | + "## convert units into a format understood by pint\n", |
| 48 | + "\n", |
| 49 | + "<div class=\"alert alert-info\">\n", |
| 50 | + "<strong>Note:</strong> this example uses the data provided by the <code>xarray.tutorial</code> functions. As such, the <code>units</code> attributes follow the CF conventions, which <code>pint</code> does not understand by default. To work around that, we are modifying the <code>units</code> attributes here, but in general it is better to use a library that adds support for the units used by the CF conventions to <code>pint</code>.\n", |
| 51 | + "</div>" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": null, |
| 57 | + "id": "published-powell", |
| 58 | + "metadata": {}, |
| 59 | + "outputs": [], |
| 60 | + "source": [ |
| 61 | + "data.lat.attrs[\"units\"] = \"degree\"\n", |
| 62 | + "data.lon.attrs[\"units\"] = \"degree\"" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "markdown", |
| 67 | + "id": "banned-tolerance", |
| 68 | + "metadata": {}, |
| 69 | + "source": [ |
| 70 | + "## quantify the data" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "code", |
| 75 | + "execution_count": null, |
| 76 | + "id": "divine-boost", |
| 77 | + "metadata": {}, |
| 78 | + "outputs": [], |
| 79 | + "source": [ |
| 80 | + "quantified = data.pint.quantify()\n", |
| 81 | + "quantified" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "markdown", |
| 86 | + "id": "whole-momentum", |
| 87 | + "metadata": {}, |
| 88 | + "source": [ |
| 89 | + "## work with the data" |
| 90 | + ] |
| 91 | + }, |
| 92 | + { |
| 93 | + "cell_type": "code", |
| 94 | + "execution_count": null, |
| 95 | + "id": "dried-friday", |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [], |
| 98 | + "source": [ |
| 99 | + "monthly_means = (\n", |
| 100 | + " quantified\n", |
| 101 | + " .pint.to(\"degC\")\n", |
| 102 | + " .sel(time=\"2013\")\n", |
| 103 | + " .groupby(\"time.month\").mean()\n", |
| 104 | + ")\n", |
| 105 | + "monthly_means" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "markdown", |
| 110 | + "id": "still-ebony", |
| 111 | + "metadata": {}, |
| 112 | + "source": [ |
| 113 | + "## plot\n", |
| 114 | + "\n", |
| 115 | + "`xarray`'s plotting functions will cast the data to `numpy.ndarray`, so we need to \"dequantify\" first." |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "code", |
| 120 | + "execution_count": null, |
| 121 | + "id": "united-machine", |
| 122 | + "metadata": {}, |
| 123 | + "outputs": [], |
| 124 | + "source": [ |
| 125 | + "monthly_means.pint.dequantify(format=\"~P\").plot.imshow(col=\"month\", col_wrap=4)" |
| 126 | + ] |
| 127 | + } |
| 128 | + ], |
| 129 | + "metadata": { |
| 130 | + "kernelspec": { |
| 131 | + "display_name": "Python 3", |
| 132 | + "language": "python", |
| 133 | + "name": "python3" |
| 134 | + }, |
| 135 | + "language_info": { |
| 136 | + "codemirror_mode": { |
| 137 | + "name": "ipython", |
| 138 | + "version": 3 |
| 139 | + }, |
| 140 | + "file_extension": ".py", |
| 141 | + "mimetype": "text/x-python", |
| 142 | + "name": "python", |
| 143 | + "nbconvert_exporter": "python", |
| 144 | + "pygments_lexer": "ipython3", |
| 145 | + "version": "3.8.6" |
| 146 | + } |
| 147 | + }, |
| 148 | + "nbformat": 4, |
| 149 | + "nbformat_minor": 5 |
| 150 | +} |
0 commit comments