Skip to content

Commit 37d3abf

Browse files
Merge pull request #821 from sadielbartholomew/student-recipes-4
Add new recipe (19) as started by summer student: per-season trends
2 parents b5c40da + d99bfd5 commit 37d3abf

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
"""
2+
Plotting per-season trends in global sea surface tempreature extrema
3+
====================================================================
4+
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.
10+
"""
11+
12+
# %%
13+
# 1. Import cf-python, cf-plot and other required packages:
14+
import cfplot as cfp
15+
import matplotlib.pyplot as plt
16+
17+
import cf
18+
19+
# %%
20+
# 2. Read the dataset in extract the SST Field from the FieldList:
21+
f = cf.read("~/recipes/ERA5_monthly_averaged_SST.nc")
22+
sst = f[0] # this gives the sea surface temperature (SST)
23+
24+
# %%
25+
# 3. Collapse the SST data by area extrema (extrema 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"
28+
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:
33+
am_max = am_max.subspace(T=cf.ge(cf.dt("1980-01-01")))
34+
am_min = am_min.subspace(T=cf.ge(cf.dt("1980-01-01")))
35+
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:
41+
colours_seasons_mapping = {
42+
"red": (cf.mam(), "Mean across MAM: March, April and May"),
43+
"blue": (cf.jja(), "Mean across JJA: June, July and August"),
44+
"green": (cf.son(), "Mean across SON: September, October and November"),
45+
"purple": (cf.djf(), "Mean across DJF: December, January and February"),
46+
}
47+
48+
# %%
49+
# 6. Create and open the plot file:
50+
cfp.gopen(
51+
rows=2, columns=1, bottom=0.1, top=0.85, file="global_avg_sst_plot.png"
52+
)
53+
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:
62+
cfp.gpos(1)
63+
cfp.gset(xmin="1980-01-01", xmax="2022-12-01", ymin=304, ymax=312)
64+
for colour, season_query in colours_seasons_mapping.items():
65+
query_on_season, season_description = season_query
66+
am_max_collapse = am_max.collapse("T: mean", group=query_on_season)
67+
cfp.lineplot(
68+
am_max_collapse,
69+
color=colour,
70+
markeredgecolor=colour,
71+
marker="o",
72+
label=season_description,
73+
title="Maxima per month or season",
74+
)
75+
cfp.lineplot(
76+
am_max,
77+
color="grey",
78+
xlabel="",
79+
label="All months",
80+
)
81+
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:
86+
cfp.gpos(2)
87+
cfp.gset(xmin="1980-01-01", xmax="2022-12-01", ymin=269, ymax=272)
88+
for colour, season_query in colours_seasons_mapping.items():
89+
query_on_season, season_description = season_query
90+
am_min_collapse = am_min.collapse("T: mean", group=query_on_season)
91+
cfp.lineplot(
92+
am_min_collapse,
93+
color=colour,
94+
markeredgecolor=colour,
95+
marker="o",
96+
xlabel="",
97+
title="Minima per month or season",
98+
)
99+
cfp.lineplot(
100+
am_min,
101+
color="grey",
102+
)
103+
104+
# %%
105+
# 9. Add an overall title to the plot and close the file to save it:
106+
plt.suptitle(
107+
"Global mean sea surface temperature (SST) monthly\nminima and maxima "
108+
"showing seasonal means of these extrema",
109+
fontsize=18,
110+
)
111+
cfp.gclose()

docs/source/recipes/recipe_list.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ plot_17_recipe.html#sphx-glr-recipes-plot-17-recipe-py
3434
<div class="sphx-glr-thumbcontainer contourmap subspace subplot" tooltip="Contourmap, Subspace, Subplot">
3535
plot_18_recipe.html#sphx-glr-recipes-plot-18-recipe-py
3636
<div class="sphx-glr-thumbcontainer regrid stats" tooltip="Regrid, Statistical Operations">
37+
plot_19_recipe.html#sphx-glr-recipes-plot-19-recipe-py
38+
<div class="sphx-glr-thumbcontainer collapse subspace subplot lineplot" tooltip="Collapse, Subspace, Subplot, Lineplot">

0 commit comments

Comments
 (0)