Skip to content

Commit f09d42e

Browse files
committed
MNT: change to environment.yml, add jupytext and add markdown versions for easier review
1 parent 89a2a88 commit f09d42e

6 files changed

+320
-30
lines changed

Diff for: MatplotlibExample.ipynb

+3
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@
270270
}
271271
],
272272
"metadata": {
273+
"jupytext": {
274+
"formats": "ipynb,md"
275+
},
273276
"kernelspec": {
274277
"display_name": "Python 3 (ipykernel)",
275278
"language": "python",

Diff for: MatplotlibExample.md

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
formats: ipynb,md
5+
text_representation:
6+
extension: .md
7+
format_name: markdown
8+
format_version: '1.3'
9+
jupytext_version: 1.13.0
10+
kernelspec:
11+
display_name: Python 3 (ipykernel)
12+
language: python
13+
name: python3
14+
---
15+
16+
# Matplotlib interactive examples
17+
18+
This is a small example of using Matplotlib for interactive data visualization.
19+
20+
To get interactive visualization you must "run" the notebook cells (in order). You can hit the "play" triangle above, or the "play-all" double triangle to run all the cells, or select a cell and hit "Shift-Enter". Once active, you can zoom and pan in the provided data using the GUI toolbar at left of each figure.
21+
22+
```python
23+
import numpy as np
24+
import matplotlib.pyplot as plt
25+
26+
# this sets up the Matplotlib interactive windows:
27+
%matplotlib widget
28+
29+
# this changes the default date converter for better interactive plotting of dates:
30+
plt.rcParams['date.converter'] = 'concise'
31+
```
32+
33+
## Time series example: temperature at Dinosaur, Co
34+
35+
Data is from https://www.ncei.noaa.gov/pub/data/uscrn/products/hourly02/ and is hourly temperature and solar radiation at Dinosaur, Colorado.
36+
37+
### Load the data
38+
39+
```python
40+
# Note the use of datetimes in the file complicate loading a bit.
41+
# We recommend using pandas or xarray for more elegant solutions
42+
# to handling complex timeseries data.
43+
with open('data/small.txt', 'r') as f:
44+
data = np.genfromtxt(f, dtype='datetime64[s],f,f,f',
45+
names=['date', 'doy', 'temp', 'solar'])
46+
datetime = data['date']
47+
dayofyear = data['doy']
48+
temperature = data['temp']
49+
solar = data['solar']
50+
51+
# make two-day smoothed versions:
52+
temp_low = np.convolve(temperature, np.ones(48)/48, mode='same')
53+
solar_low = np.convolve(solar, np.ones(48)/48, mode='same')
54+
```
55+
56+
### Plot timeseries
57+
58+
Here we plot the time series, add labels, title, and a small annotation about where the data came from. Using the toolbar on the left you can zoom in on the data to see, for instance, the day/night warming and cooling. You can also pan along the time series using the icon with four arrows. Note that because we have said `sharex=True` when creating the axes, the time window remains connected in both time seroes.
59+
60+
```python
61+
fig, (ax0, ax1) = plt.subplots(2, 1, sharex=True, constrained_layout=True)
62+
63+
# temperature:
64+
ax0.plot(datetime, temperature, label='hourly')
65+
ax0.plot(datetime, temp_low, label='smoothed')
66+
ax0.legend(loc='upper right')
67+
ax0.set_ylabel('Temperature $[^oC]$') # note the use of TeX math formatting
68+
69+
# solar-radiation:
70+
ax1.plot(datetime, solar, label='hourly')
71+
ax1.plot(datetime, solar_low, label='smoothed')
72+
ax1.legend(loc='upper right')
73+
ax1.set_ylabel('Solar radiation $[W\,m^{-2}]$') # note the use of TeX math formatting
74+
75+
ax0.set_title('Observations: Dinosaur, Colorado', loc='left')
76+
ax0.text(0.03, 0.03, 'https://www.ncei.noaa.gov/pub/data/uscrn/products/hourly02/',
77+
fontsize='small', fontstyle='italic', transform=ax0.transAxes);
78+
```
79+
80+
### Plot property-versus-property
81+
82+
Lets make a scatter plot of solar radiation versus temperature, colored by day of the year. Note that we do not share the axes this time because the dynamic range of the hourly and smoothed data is different.
83+
84+
```python
85+
fig, [ax0, ax1] = plt.subplots(1, 2, constrained_layout=True, figsize=(8, 4))
86+
87+
ax0.set_facecolor('0.6')
88+
sc = ax0.scatter(solar, temperature, c=dayofyear, s=2, cmap='RdBu_r')
89+
ax0.set_xlabel('Solar Radiation $[W\,m^{-2}]$')
90+
ax0.set_ylabel('Temperature $[^oC]$')
91+
ax0.set_title('Hourly', loc='left')
92+
93+
94+
ax1.set_facecolor('0.6')
95+
sc = ax1.scatter(solar_low[::24], temp_low[::24], c=dayofyear[::24], s=4, cmap='RdBu_r')
96+
ax1.set_xlabel('Solar Radiation $[W\,m^{-2}]$')
97+
ax1.set_ylabel('Temperature $[^oC]$')
98+
ax1.set_title('Smoothed', loc='left')
99+
100+
fig.colorbar(sc, ax=[ax0, ax1], shrink=0.6, label='day of year')
101+
```
102+
103+
## Image data: global land-surface temperature
104+
105+
Matplotlib can also plot two-dimensional data as in this example of land surface temperature. Note that for geographic data a package like cartopy or metpy is recommended.
106+
107+
### Load the data
108+
109+
```python
110+
dat = np.genfromtxt('data/MOD_LSTD_E_2021-08-29_gs_720x360.CSV', delimiter=',')
111+
dat = np.where(dat<1000, dat, np.NaN)
112+
dat = dat[::-1, :]
113+
lon = np.arange(-180.0, 180.1, 0.5)
114+
lat = np.arange(-90.0, 90.1, 0.5)
115+
date = '2021-08-29 to 2021-09-05'
116+
source = 'https://neo.sci.gsfc.nasa.gov/view.php?datasetId=MOD_LSTD_E&date=2021-09-01'
117+
```
118+
119+
### Plot data
120+
121+
```python
122+
fig, ax = plt.subplots(constrained_layout=True, figsize=(7, 4))
123+
ax.set_facecolor('0.8')
124+
pc = ax.pcolormesh(lon, lat, dat, shading='auto', cmap='inferno')
125+
ax.set_aspect(1.3)
126+
ax.set_xlabel('Longitude $[^o E]$')
127+
ax.set_ylabel('Latitude $[^o N]$')
128+
fig.colorbar(pc, shrink=0.6, extend='both', label='land temperature $[^oC]$');
129+
ax.set_title(f'source: {source}', loc='left', fontsize='x-small');
130+
```
131+
132+
```python
133+
134+
```

Diff for: _PreProcess.ipynb

+20-27
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
},
6969
{
7070
"cell_type": "code",
71-
"execution_count": 30,
71+
"execution_count": 2,
7272
"id": "26404dbb",
7373
"metadata": {},
7474
"outputs": [
@@ -78,14 +78,14 @@
7878
"Text(0.5, 1.0, 'Dinosaur, Colorado')"
7979
]
8080
},
81-
"execution_count": 30,
81+
"execution_count": 2,
8282
"metadata": {},
8383
"output_type": "execute_result"
8484
},
8585
{
8686
"data": {
8787
"application/vnd.jupyter.widget-view+json": {
88-
"model_id": "489f1a87d14c46eda1d85b3a79fadc64",
88+
"model_id": "b2d02da8fdd146808b5606ca05308db9",
8989
"version_major": 2,
9090
"version_minor": 0
9191
},
@@ -140,7 +140,7 @@
140140
},
141141
{
142142
"cell_type": "code",
143-
"execution_count": 50,
143+
"execution_count": 3,
144144
"id": "14043e75-6808-4b20-b817-e8f5de887deb",
145145
"metadata": {},
146146
"outputs": [
@@ -156,17 +156,17 @@
156156
{
157157
"data": {
158158
"text/plain": [
159-
"[<matplotlib.lines.Line2D at 0x130709160>]"
159+
"[<matplotlib.lines.Line2D at 0x18947bca0>]"
160160
]
161161
},
162-
"execution_count": 50,
162+
"execution_count": 3,
163163
"metadata": {},
164164
"output_type": "execute_result"
165165
},
166166
{
167167
"data": {
168168
"application/vnd.jupyter.widget-view+json": {
169-
"model_id": "428ae0720c2442568d8c41d59005a965",
169+
"model_id": "15167a2550384f5db6c34352a1500e26",
170170
"version_major": 2,
171171
"version_minor": 0
172172
},
@@ -200,7 +200,7 @@
200200
},
201201
{
202202
"cell_type": "code",
203-
"execution_count": 53,
203+
"execution_count": 4,
204204
"id": "b85880f8-9d99-43e2-a17f-d7cd0850a787",
205205
"metadata": {},
206206
"outputs": [],
@@ -215,20 +215,10 @@
215215
},
216216
{
217217
"cell_type": "code",
218-
"execution_count": 37,
218+
"execution_count": null,
219219
"id": "daa12c24-eeac-4657-9530-1e5fbcd82b20",
220220
"metadata": {},
221-
"outputs": [
222-
{
223-
"name": "stdout",
224-
"output_type": "stream",
225-
"text": [
226-
"CRNH0203-2018-CO_Dinosaur_2_E.txt all.txt\n",
227-
"CRNH0203-2019-CO_Dinosaur_2_E.txt small.txt\n",
228-
"CRNH0203-2020-CO_Dinosaur_2_E.txt\n"
229-
]
230-
}
231-
],
221+
"outputs": [],
232222
"source": []
233223
},
234224
{
@@ -243,7 +233,7 @@
243233
},
244234
{
245235
"cell_type": "code",
246-
"execution_count": 2,
236+
"execution_count": 5,
247237
"id": "27986c69-5df6-48f8-a309-64a1a9ffce0d",
248238
"metadata": {},
249239
"outputs": [],
@@ -253,7 +243,7 @@
253243
},
254244
{
255245
"cell_type": "code",
256-
"execution_count": 12,
246+
"execution_count": 6,
257247
"id": "b818784e-4769-4f0c-93fd-63ff1d38323b",
258248
"metadata": {},
259249
"outputs": [],
@@ -267,32 +257,32 @@
267257
},
268258
{
269259
"cell_type": "code",
270-
"execution_count": 13,
260+
"execution_count": 7,
271261
"id": "df2f48a4-fc21-48cd-8ceb-076fb51020be",
272262
"metadata": {},
273263
"outputs": [
274264
{
275265
"name": "stderr",
276266
"output_type": "stream",
277267
"text": [
278-
"/var/folders/vy/zxjl84j90ws6d1k09_rhst0w0000gp/T/ipykernel_42405/3708665128.py:4: MatplotlibDeprecationWarning: shading='flat' when X and Y have the same dimensions as C is deprecated since 3.3. Either specify the corners of the quadrilaterals with X and Y, or pass shading='auto', 'nearest' or 'gouraud', or set rcParams['pcolor.shading']. This will become an error two minor releases later.\n",
268+
"/var/folders/vy/zxjl84j90ws6d1k09_rhst0w0000gp/T/ipykernel_61351/1084180396.py:2: MatplotlibDeprecationWarning: shading='flat' when X and Y have the same dimensions as C is deprecated since 3.3. Either specify the corners of the quadrilaterals with X and Y, or pass shading='auto', 'nearest' or 'gouraud', or set rcParams['pcolor.shading']. This will become an error two minor releases later.\n",
279269
" ax.pcolormesh(lon, lat, dat)\n"
280270
]
281271
},
282272
{
283273
"data": {
284274
"text/plain": [
285-
"<matplotlib.collections.QuadMesh at 0x1349f39a0>"
275+
"<matplotlib.collections.QuadMesh at 0x18949ed60>"
286276
]
287277
},
288-
"execution_count": 13,
278+
"execution_count": 7,
289279
"metadata": {},
290280
"output_type": "execute_result"
291281
},
292282
{
293283
"data": {
294284
"application/vnd.jupyter.widget-view+json": {
295-
"model_id": "c44373028ee440199d202e48c67496d0",
285+
"model_id": "036afc53c7854ee0ac3a49885a1f30b3",
296286
"version_major": 2,
297287
"version_minor": 0
298288
},
@@ -329,6 +319,9 @@
329319
}
330320
],
331321
"metadata": {
322+
"jupytext": {
323+
"formats": "ipynb,md"
324+
},
332325
"kernelspec": {
333326
"display_name": "Python 3 (ipykernel)",
334327
"language": "python",

0 commit comments

Comments
 (0)