Skip to content

Commit 9aeadc3

Browse files
Flesh out literate prog. comments for recipe 19
1 parent 24e4f92 commit 9aeadc3

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

docs/source/recipes/plot_19_recipe.py

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,65 @@
11
"""
2-
Recipe 1: Calculating and Plotting Seasonal Mean Pressure at Mean Sea Level
2+
Calculate and plotting per-season trends in global sea surface extrema
3+
======================================================================
34
4-
Objective: Calculate and plot the seasonal mean pressure at mean sea level
5-
from ensemble simulation data for 1941.
5+
In this recipe we find the area-based extrema of global sea surface
6+
temperature per month and, because it is very difficult to
7+
interpret for trends when in a monthly form, we calculate and plot
8+
on top of this the mean across each season for both the minima and the
9+
maxima.
610
"""
711

12+
# %%
13+
# 1. Import cf-python, cf-plot and other required packages:
814
import cfplot as cfp
915
import matplotlib.pyplot as plt
1016

1117
import cf
1218

13-
# 1. Load the dataset
19+
# %%
20+
# 2. Read the dataset in extract the SST Field from the FieldList:
1421
f = cf.read("~/recipes_break/ERA5_monthly_averaged_SST.nc")
15-
sst = f[0] # Select the SST variable
22+
sst = f[0]
1623

17-
# Collapse data by area mean (average over spatial dimensions)
18-
am_max = sst.collapse("area: maximum") # equivalent to "X Y: mean"
19-
am_min = sst.collapse("area: minimum")
24+
# %%
25+
# 3. Collapse data by area extrema (average over spatial dimensions):
26+
am_max = sst.collapse("area: maximum") # equivalent to "X Y: maximum"
27+
am_min = sst.collapse("area: minimum") # equivalent to "X Y: minimum"
2028

21-
# Reduce all timeseries down to just 1980+ since there are some data
22-
# quality issues before 1970
29+
# %%
30+
# 4. Reduce all timeseries down to just 1980+ since there are some data
31+
# quality issues before 1970 and also this window is about perfect size
32+
# for viewing the trends without the line plot becoming too cluttered:
2333
am_max = am_max.subspace(T=cf.ge(cf.dt("1980-01-01")))
2434
am_min = am_min.subspace(T=cf.ge(cf.dt("1980-01-01")))
2535

26-
# TODO COMMENT
36+
# %%
37+
# 5. Create a mapping which provides the queries we need to collapse on
38+
# the four seasons, along with our description of them, as a value, with
39+
# the key of the string encoding the colour we want to plot these
40+
# trendlines in. This structure will be iterated over to make our plot:
2741
colours_seasons_map = {
2842
"red": (cf.mam(), "Mean across MAM: March, April and May"),
2943
"blue": (cf.jja(), "Mean across JJA: June, July and August"),
3044
"green": (cf.son(), "Mean across SON: September, October and November"),
3145
"purple": (cf.djf(), "Mean across DJF: December, January and February"),
3246
}
3347

48+
# %%
49+
# 6. Create and open the plot file:
3450
cfp.gopen(
3551
rows=2, columns=1, bottom=0.1, top=0.85, file="global_avg_sst_plot.png"
3652
)
3753

38-
# Put maxima subplot at top since these values are higher, given
39-
# increasing x axis
54+
# %%
55+
# 7. Put maxima subplot at top since these values are higher, given
56+
# increasing x axis. Note we set limits manually with 'gset' only to
57+
# allow space so the legend doesn't overlap the data, which isn't
58+
# possible purely from positioning it anywhere within the default plot.
59+
# Otherwise cf-plot handles this for us. To plot the per-season means
60+
# of the maxima, we loop through the season query mapping and do a
61+
# "T: mean" collapse setting the season as the grouping:
4062
cfp.gpos(1)
41-
plt.suptitle(
42-
"Global Average Sea Surface Temperature monthly minima\nand maxima "
43-
"including seasonal means of these extrema",
44-
fontsize=18,
45-
)
46-
# Set limits manually only to allow space so the legend doesn't overlap the
47-
# data, which isn't possible purely from positioning it anywhere
4863
cfp.gset(xmin="1980-01-01", xmax="2022-12-01", ymin=304, ymax=312)
4964
for colour, season_query in colours_seasons_map.items():
5065
query_on_season, season_description = season_query
@@ -64,7 +79,10 @@
6479
label="All months",
6580
)
6681

67-
# Minima subplot below the maxima one
82+
# %%
83+
# 8. Create and add minima subplot below the maxima one. Just like for the
84+
# maxima case, we plot per-season means by looping through the season query
85+
# mapping and doing a "T: mean" collapse setting the season as the grouping:
6886
cfp.gpos(2)
6987
cfp.gset(xmin="1980-01-01", xmax="2022-12-01", ymin=269, ymax=272)
7088
for colour, season_query in colours_seasons_map.items():
@@ -83,4 +101,11 @@
83101
color="grey",
84102
)
85103

104+
# %%
105+
# 9. Add an overall title to the plot and close the file to save it:
106+
plt.suptitle(
107+
"Global Average Sea Surface Temperature monthly minima\nand maxima "
108+
"including seasonal means of these extrema",
109+
fontsize=18,
110+
)
86111
cfp.gclose()

0 commit comments

Comments
 (0)