Skip to content

Commit 789a3d2

Browse files
rabernatkeewis
authored andcommitted
Replace sphinx_gallery with notebook (pydata#3106)
* switching out examples to use nbsphinx * added jupyter_client to doc env * moved gallery to notebook
1 parent e574663 commit 789a3d2

File tree

2 files changed

+297
-1
lines changed

2 files changed

+297
-1
lines changed

doc/examples.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ Examples
77
examples/weather-data
88
examples/monthly-means
99
examples/multidimensional-coords
10-
auto_gallery/index
10+
examples/visualization_gallery
Lines changed: 296 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,296 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Visualization Gallery\n",
8+
"\n",
9+
"This notebook shows common visualization issues encountered in Xarray."
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"import cartopy.crs as ccrs\n",
19+
"import matplotlib.pyplot as plt\n",
20+
"import xarray as xr\n",
21+
"%matplotlib inline"
22+
]
23+
},
24+
{
25+
"cell_type": "markdown",
26+
"metadata": {},
27+
"source": [
28+
"Load example dataset:"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": null,
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"ds = xr.tutorial.load_dataset('air_temperature')"
38+
]
39+
},
40+
{
41+
"cell_type": "markdown",
42+
"metadata": {},
43+
"source": [
44+
"## Multiple plots and map projections\n",
45+
"\n",
46+
"Control the map projection parameters on multiple axes\n",
47+
"\n",
48+
"This example illustrates how to plot multiple maps and control their extent\n",
49+
"and aspect ratio.\n",
50+
"\n",
51+
"For more details see [this discussion](https://github.com/pydata/xarray/issues/1397#issuecomment-299190567) on github."
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"air = ds.air.isel(time=[0, 724]) - 273.15\n",
61+
"\n",
62+
"# This is the map projection we want to plot *onto*\n",
63+
"map_proj = ccrs.LambertConformal(central_longitude=-95, central_latitude=45)\n",
64+
"\n",
65+
"p = air.plot(transform=ccrs.PlateCarree(), # the data's projection\n",
66+
" col='time', col_wrap=1, # multiplot settings\n",
67+
" aspect=ds.dims['lon'] / ds.dims['lat'], # for a sensible figsize\n",
68+
" subplot_kws={'projection': map_proj}) # the plot's projection\n",
69+
"\n",
70+
"# We have to set the map's options on all axes\n",
71+
"for ax in p.axes.flat:\n",
72+
" ax.coastlines()\n",
73+
" ax.set_extent([-160, -30, 5, 75])"
74+
]
75+
},
76+
{
77+
"cell_type": "markdown",
78+
"metadata": {},
79+
"source": [
80+
"## Centered colormaps\n",
81+
"\n",
82+
"Xarray's automatic colormaps choice"
83+
]
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": null,
88+
"metadata": {},
89+
"outputs": [],
90+
"source": [
91+
"air = ds.air.isel(time=0)\n",
92+
"\n",
93+
"f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(8, 6))\n",
94+
"\n",
95+
"# The first plot (in kelvins) chooses \"viridis\" and uses the data's min/max\n",
96+
"air.plot(ax=ax1, cbar_kwargs={'label': 'K'})\n",
97+
"ax1.set_title('Kelvins: default')\n",
98+
"ax2.set_xlabel('')\n",
99+
"\n",
100+
"# The second plot (in celsius) now chooses \"BuRd\" and centers min/max around 0\n",
101+
"airc = air - 273.15\n",
102+
"airc.plot(ax=ax2, cbar_kwargs={'label': '°C'})\n",
103+
"ax2.set_title('Celsius: default')\n",
104+
"ax2.set_xlabel('')\n",
105+
"ax2.set_ylabel('')\n",
106+
"\n",
107+
"# The center doesn't have to be 0\n",
108+
"air.plot(ax=ax3, center=273.15, cbar_kwargs={'label': 'K'})\n",
109+
"ax3.set_title('Kelvins: center=273.15')\n",
110+
"\n",
111+
"# Or it can be ignored\n",
112+
"airc.plot(ax=ax4, center=False, cbar_kwargs={'label': '°C'})\n",
113+
"ax4.set_title('Celsius: center=False')\n",
114+
"ax4.set_ylabel('')\n",
115+
"\n",
116+
"# Make it nice\n",
117+
"plt.tight_layout()"
118+
]
119+
},
120+
{
121+
"cell_type": "markdown",
122+
"metadata": {},
123+
"source": [
124+
"## Control the plot's colorbar\n",
125+
"\n",
126+
"Use ``cbar_kwargs`` keyword to specify the number of ticks.\n",
127+
"The ``spacing`` kwarg can be used to draw proportional ticks."
128+
]
129+
},
130+
{
131+
"cell_type": "code",
132+
"execution_count": null,
133+
"metadata": {},
134+
"outputs": [],
135+
"source": [
136+
"air2d = ds.air.isel(time=500)\n",
137+
"\n",
138+
"# Prepare the figure\n",
139+
"f, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(14, 4))\n",
140+
"\n",
141+
"# Irregular levels to illustrate the use of a proportional colorbar\n",
142+
"levels = [245, 250, 255, 260, 265, 270, 275, 280, 285, 290, 310, 340]\n",
143+
"\n",
144+
"# Plot data\n",
145+
"air2d.plot(ax=ax1, levels=levels)\n",
146+
"air2d.plot(ax=ax2, levels=levels, cbar_kwargs={'ticks': levels})\n",
147+
"air2d.plot(ax=ax3, levels=levels, cbar_kwargs={'ticks': levels,\n",
148+
" 'spacing': 'proportional'})\n",
149+
"\n",
150+
"# Show plots\n",
151+
"plt.tight_layout()"
152+
]
153+
},
154+
{
155+
"cell_type": "markdown",
156+
"metadata": {},
157+
"source": [
158+
"## Multiple lines from a 2d DataArray\n",
159+
"\n",
160+
"Use ``xarray.plot.line`` on a 2d DataArray to plot selections as\n",
161+
"multiple lines.\n",
162+
"\n",
163+
"See ``plotting.multiplelines`` for more details."
164+
]
165+
},
166+
{
167+
"cell_type": "code",
168+
"execution_count": null,
169+
"metadata": {},
170+
"outputs": [],
171+
"source": [
172+
"air = ds.air - 273.15 # to celsius\n",
173+
"\n",
174+
"# Prepare the figure\n",
175+
"f, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 4), sharey=True)\n",
176+
"\n",
177+
"# Selected latitude indices\n",
178+
"isel_lats = [10, 15, 20]\n",
179+
"\n",
180+
"# Temperature vs longitude plot - illustrates the \"hue\" kwarg\n",
181+
"air.isel(time=0, lat=isel_lats).plot.line(ax=ax1, hue='lat')\n",
182+
"ax1.set_ylabel('°C')\n",
183+
"\n",
184+
"# Temperature vs time plot - illustrates the \"x\" and \"add_legend\" kwargs\n",
185+
"air.isel(lon=30, lat=isel_lats).plot.line(ax=ax2, x='time', add_legend=False)\n",
186+
"ax2.set_ylabel('')\n",
187+
"\n",
188+
"# Show\n",
189+
"plt.tight_layout()"
190+
]
191+
},
192+
{
193+
"cell_type": "markdown",
194+
"metadata": {},
195+
"source": [
196+
"## `imshow()` and rasterio map projections\n",
197+
"\n",
198+
"\n",
199+
"Using rasterio's projection information for more accurate plots.\n",
200+
"\n",
201+
"This example extends `recipes.rasterio` and plots the image in the\n",
202+
"original map projection instead of relying on pcolormesh and a map\n",
203+
"transformation."
204+
]
205+
},
206+
{
207+
"cell_type": "code",
208+
"execution_count": null,
209+
"metadata": {},
210+
"outputs": [],
211+
"source": [
212+
"url = 'https://github.com/mapbox/rasterio/raw/master/tests/data/RGB.byte.tif'\n",
213+
"da = xr.open_rasterio(url)\n",
214+
"\n",
215+
"# The data is in UTM projection. We have to set it manually until\n",
216+
"# https://github.com/SciTools/cartopy/issues/813 is implemented\n",
217+
"crs = ccrs.UTM('18N')\n",
218+
"\n",
219+
"# Plot on a map\n",
220+
"ax = plt.subplot(projection=crs)\n",
221+
"da.plot.imshow(ax=ax, rgb='band', transform=crs)\n",
222+
"ax.coastlines('10m', color='r')"
223+
]
224+
},
225+
{
226+
"cell_type": "markdown",
227+
"metadata": {},
228+
"source": [
229+
"## Parsing rasterio's geocoordinates\n",
230+
"\n",
231+
"Converting a projection's cartesian coordinates into 2D longitudes and\n",
232+
"latitudes.\n",
233+
"\n",
234+
"These new coordinates might be handy for plotting and indexing, but it should\n",
235+
"be kept in mind that a grid which is regular in projection coordinates will\n",
236+
"likely be irregular in lon/lat. It is often recommended to work in the data's\n",
237+
"original map projection (see `recipes.rasterio_rgb`)."
238+
]
239+
},
240+
{
241+
"cell_type": "code",
242+
"execution_count": null,
243+
"metadata": {},
244+
"outputs": [],
245+
"source": [
246+
"from rasterio.warp import transform\n",
247+
"import numpy as np\n",
248+
"\n",
249+
"url = 'https://github.com/mapbox/rasterio/raw/master/tests/data/RGB.byte.tif'\n",
250+
"da = xr.open_rasterio(url)\n",
251+
"\n",
252+
"# Compute the lon/lat coordinates with rasterio.warp.transform\n",
253+
"ny, nx = len(da['y']), len(da['x'])\n",
254+
"x, y = np.meshgrid(da['x'], da['y'])\n",
255+
"\n",
256+
"# Rasterio works with 1D arrays\n",
257+
"lon, lat = transform(da.crs, {'init': 'EPSG:4326'},\n",
258+
" x.flatten(), y.flatten())\n",
259+
"lon = np.asarray(lon).reshape((ny, nx))\n",
260+
"lat = np.asarray(lat).reshape((ny, nx))\n",
261+
"da.coords['lon'] = (('y', 'x'), lon)\n",
262+
"da.coords['lat'] = (('y', 'x'), lat)\n",
263+
"\n",
264+
"# Compute a greyscale out of the rgb image\n",
265+
"greyscale = da.mean(dim='band')\n",
266+
"\n",
267+
"# Plot on a map\n",
268+
"ax = plt.subplot(projection=ccrs.PlateCarree())\n",
269+
"greyscale.plot(ax=ax, x='lon', y='lat', transform=ccrs.PlateCarree(),\n",
270+
" cmap='Greys_r', add_colorbar=False)\n",
271+
"ax.coastlines('10m', color='r')"
272+
]
273+
}
274+
],
275+
"metadata": {
276+
"kernelspec": {
277+
"display_name": "Python 3",
278+
"language": "python",
279+
"name": "python3"
280+
},
281+
"language_info": {
282+
"codemirror_mode": {
283+
"name": "ipython",
284+
"version": 3
285+
},
286+
"file_extension": ".py",
287+
"mimetype": "text/x-python",
288+
"name": "python",
289+
"nbconvert_exporter": "python",
290+
"pygments_lexer": "ipython3",
291+
"version": "3.7.3"
292+
}
293+
},
294+
"nbformat": 4,
295+
"nbformat_minor": 4
296+
}

0 commit comments

Comments
 (0)