Skip to content

Commit 7b03a3a

Browse files
authored
add a plotting example (#61)
* add a plotting example * add matplotlib to the dependencies * configure nbsphinx * fail RTD build on warnings * use the correct name for matplotlib * also depend on ipykernel and jupyter_client * use alert-warning for the note * use "alert alert-info" instead * try to fix the info block * try if removing the ' helps * fix the alert markup
1 parent 50291f9 commit 7b03a3a

File tree

5 files changed

+166
-1
lines changed

5 files changed

+166
-1
lines changed

docs/conf.py

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"sphinx_autosummary_accessors",
4747
"IPython.sphinxext.ipython_directive",
4848
"IPython.sphinxext.ipython_console_highlighting",
49+
"nbsphinx",
4950
]
5051

5152
# Add any paths that contain templates here, relative to this directory.
@@ -100,6 +101,10 @@
100101
"unit-like": ":term:`unit-like`",
101102
}
102103

104+
# nbsphinx
105+
nbsphinx_timeout = 600
106+
nbsphinx_execute = "always"
107+
103108
# -- Options for intersphinx extension ---------------------------------------
104109

105110
intersphinx_mapping = {

docs/examples.rst

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
Examples
22
========
3-
There are no examples yet.
3+
4+
.. toctree::
5+
:maxdepth: 1
6+
7+
examples/plotting

docs/examples/plotting.ipynb

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
}

docs/requirements.txt

+3
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ netCDF4
44
sphinx>=3.2
55
sphinx_rtd_theme
66
ipython
7+
ipykernel
8+
jupyter_client
79
nbsphinx
10+
matplotlib
811
sphinx-autosummary-accessors

readthedocs.yml

+3
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ python:
88
- requirements: docs/requirements.txt
99
- method: pip
1010
path: .
11+
12+
sphinx:
13+
fail_on_warning: true

0 commit comments

Comments
 (0)