Skip to content

Commit 43371e4

Browse files
Update NH original recipe 17 to show each cmap category w/ one plot
1 parent e18b90b commit 43371e4

File tree

1 file changed

+80
-22
lines changed

1 file changed

+80
-22
lines changed

docs/source/recipes/plot_17_recipe.py

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
# 1. Import cf-python and cf-plot:
1313

1414
import cfplot as cfp
15+
import matplotlib.pyplot as plt
1516

1617
import cf
1718

1819
# %%
1920
# 2. Read the field in:
2021
# Here I've used sample data ggap.nc (and later pressure=850), but you
2122
# could use tas_A1.nc (with time=15)
22-
PATH="~/git-repos/cf-plot/cfplot/test/cfplot_data"
23+
PATH = "~/git-repos/cf-plot/cfplot/test/cfplot_data"
2324
f = cf.read(f"{PATH}/ggap.nc")[0]
2425

2526
# %%
@@ -30,58 +31,115 @@
3031
# Simply change the name in quotes and ensure the
3132
# number of rows * number of columns = number of colour scales
3233

34+
3335
# %%
3436
# a. Perceptually uniform colour scales, with no zero value
35-
colour_scale = ["viridis", "magma", "inferno", "plasma", "parula", "gray"]
36-
cfp.gopen(rows=2, columns=3, bottom=0.2)
37+
colour_scale_pu = [
38+
"viridis",
39+
"magma",
40+
"inferno",
41+
] # "plasma", "parula", "gray"]
42+
3743

3844
# %%
3945
# b. NCAR Command Language - Enhanced to help with colour blindness
40-
colour_scale = [
46+
colour_scale_ncl = [
4147
"StepSeq25",
4248
"posneg_2",
43-
"posneg_1",
44-
"BlueDarkOrange18",
45-
"BlueDarkRed18",
49+
# "posneg_1",
50+
# "BlueDarkOrange18",
51+
# "BlueDarkRed18",
4652
"GreenMagenta16",
47-
"BlueGreen14",
48-
"BrownBlue12",
49-
"Cat12",
53+
# "BlueGreen14",
54+
# "BrownBlue12",
55+
# "Cat12",
5056
]
51-
cfp.gopen(rows=3, columns=3, bottom=0.1)
57+
5258

5359
# %%
5460
# c. Orography/bathymetry colour scales
5561
# These are used to show the shape/contour of landmasses, bear in mind the
5662
# example data we use is with pressure so doesnt accurately represent this.
5763
# You could instead use cfp.cscale('wiki_2_0', ncols=16, below=2, above=14)
5864
# or any other orography colour scale in a similar way.
59-
colour_scale = [
65+
colour_scale_ob = [
6066
"os250kmetres",
6167
"wiki_1_0_2",
62-
"wiki_1_0_3",
63-
"wiki_2_0",
64-
"wiki_2_0_reduced",
68+
# "wiki_1_0_3",
69+
# "wiki_2_0",
70+
# "wiki_2_0_reduced",
6571
"arctic",
6672
]
67-
cfp.gopen(rows=2, columns=3, bottom=0.2, file="ColourPlot.png")
6873

74+
75+
# We plot each category of colourmap as columns, but given the gpos
76+
# function positions subplots from left to right, row by row from the top,
77+
# we need to interleave the values in a list. We can use zip to do this.
78+
#
79+
colour_scales_columns = [
80+
val
81+
for category in zip(colour_scale_pu, colour_scale_ncl, colour_scale_ob)
82+
for val in category
83+
]
84+
85+
zip(colour_scale_pu, colour_scale_ncl, colour_scale_ob)
6986
# %%
7087
# 4. We then use a for loop to cycle through all the different colour maps:
7188
# Only gpos has 1 added because it can only take 1 as its first value,
7289
# otherwise there are errors.
73-
for i, colour_scale in enumerate(colour_scale):
90+
cfp.gopen(rows=3, columns=3, bottom=0.1, top=0.85, file="ColourPlot.png")
91+
plt.suptitle(
92+
(
93+
"Air temperature (K) at 850 mbar pressure shown in different "
94+
"colourmap categories"
95+
),
96+
fontsize=18,
97+
)
98+
for i, colour_scale in enumerate(colour_scales_columns):
7499
cfp.gpos(i + 1)
75-
cfp.mapset(proj="cyl")
76100
cfp.cscale(colour_scale)
77101
if i == len(colour_scale) + 1:
102+
# For the final plot, don't plot the colourbar across all subplots
103+
# as is the default
78104
cfp.con(
79105
f.subspace(pressure=850),
80106
lines=False,
81-
title=colour_scale,
82-
colorbar_position=[0.1, 0.1, 0.8, 0.02],
83-
colorbar_orientation="horizontal",
107+
axes=False,
108+
colorbar_drawedges=False,
109+
colorbar_title=f"Shown in '{colour_scale}'",
110+
colorbar_fraction=0.03,
111+
colorbar_fontsize=11,
84112
)
113+
elif i < 3:
114+
if i == 0:
115+
set_title = "Perceptually uniform\ncolour maps"
116+
elif i == 1:
117+
set_title = (
118+
"NCL colour maps enhanced to \nhelp with colour blindness"
119+
)
120+
elif i == 2:
121+
set_title = "Orography/bathymetry\ncolour maps"
122+
123+
cfp.con(
124+
f.subspace(pressure=850),
125+
lines=False,
126+
axes=False,
127+
title=set_title,
128+
colorbar_drawedges=False,
129+
colorbar_title=f"Shown in '{colour_scale}'",
130+
colorbar_fraction=0.03,
131+
colorbar_fontsize=11,
132+
)
133+
85134
else:
86-
cfp.con(f.subspace(pressure=850), title=colour_scale, lines=False)
135+
cfp.con(
136+
f.subspace(pressure=850),
137+
lines=False,
138+
axes=False,
139+
colorbar_drawedges=False,
140+
colorbar_title=f"Shown in '{colour_scale}'",
141+
colorbar_fontsize=11,
142+
colorbar_fraction=0.03,
143+
)
144+
87145
cfp.gclose(view=True)

0 commit comments

Comments
 (0)