Skip to content

Commit 1259c9a

Browse files
Code and comments simplification & tidy of NH recipe 17
1 parent b962c8e commit 1259c9a

File tree

1 file changed

+52
-62
lines changed

1 file changed

+52
-62
lines changed

docs/source/recipes/plot_17_recipe.py

Lines changed: 52 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
Plotting contour subplots with different colour maps/scales
33
===========================================================
44
5-
In this recipe, we will plot data with different colour maps to illustrate
6-
the importance of choosing the correct one for a plot. This is to ensure
7-
the use of perceptually uniform scales and avoid unintended bias.
8-
5+
In this recipe, we will plot the same data with different colour maps from
6+
three categories in separate subplots to illustrate the importance of
7+
choosing a suitable one for given data. To avoid unintended bias and
8+
misrepresentation, or lack of accessibility, a careful choice must be made.
99
"""
1010

1111
# %%
@@ -18,87 +18,78 @@
1818

1919
# %%
2020
# 2. Read the field in:
21-
# Here I've used sample data ggap.nc (and later pressure=850), but you
22-
# could use tas_A1.nc (with time=15)
2321
PATH = "~/git-repos/cf-plot/cfplot/test/cfplot_data"
2422
f = cf.read(f"{PATH}/ggap.nc")[0]
2523

2624
# %%
27-
# 3. Choose a set of predefined colour scales to view (based on NCAR)
28-
# Choose a set of predefined colour scales to view (based on NCAR)
29-
# You could also choose your own from
30-
# https://ncas-cms.github.io/cf-plot/build/colour_scales.html
31-
# Simply change the name in quotes and ensure the
32-
# number of rows * number of columns = number of colour scales
33-
25+
# 3. Choose a set of predefined colour scales to view. These can be chosen
26+
# from the selection at:
27+
# https://ncas-cms.github.io/cf-plot/build/colour_scales.html or you
28+
# can define your own, see:
29+
# https://ncas-cms.github.io/cf-plot/build/colour_scales.html#user-defined-colour-scales.
30+
# Here we take three colour scales each from three different general
31+
# categories, to showcase some differences in representation.
32+
# Note colour scale levels can be adjusted using 'cscale' and keywords such as:
33+
# cfp.cscale(<cmap name>, ncols=16, below=2, above=14)
3434

3535
# %%
36-
# a. Perceptually uniform colour scales, with no zero value
37-
colour_scale_pu = ["viridis", "magma", "plasma"]
38-
36+
# a. Perceptually uniform colour scales, with no zero value, see:
37+
# https://ncas-cms.github.io/cf-plot/build/colour_scales.html#perceptually-uniform-colour-scales.
38+
colour_scales_pu = ["viridis", "magma", "plasma"]
3939

4040
# %%
41-
# b. NCAR Command Language - Enhanced to help with colour blindness
42-
colour_scale_ncl = [
43-
"posneg_1",
44-
"GreenMagenta16",
45-
"StepSeq25",
46-
]
47-
41+
# b. NCAR Command Language colour scales enhanced to help with colour
42+
# blindness, see:
43+
# https://ncas-cms.github.io/cf-plot/build/colour_scales.html#ncar-command-language-enhanced-to-help-with-colour-blindness.
44+
# These colour maps are better for accessibility.
45+
colour_scales_ncl = ["posneg_1", "GreenMagenta16", "StepSeq25"]
4846

4947
# %%
50-
# c. Orography/bathymetry colour scales
51-
# These are used to show the shape/contour of landmasses, bear in mind the
52-
# example data we use is with pressure so doesnt accurately represent this.
53-
# You could instead use cfp.cscale('wiki_2_0', ncols=16, below=2, above=14)
54-
# or any other orography colour scale in a similar way.
55-
colour_scale_ob = [
56-
"wiki_1_0_2",
57-
"wiki_2_0",
58-
"wiki_2_0_reduced",
59-
]
48+
# c. Orography/bathymetry colour scales, see:
49+
# https://ncas-cms.github.io/cf-plot/build/colour_scales.html#orography-bathymetry-colour-scales.
50+
# These are used to show the shape/contour of land masses, but bear in mind the
51+
# data we show here is air temperature so doesn't represent this and
52+
# therefore it is not a good choice in this case:
53+
colour_scales_ob = ["wiki_1_0_2", "wiki_2_0", "wiki_2_0_reduced"]
6054

6155

62-
# We plot each category of colourmap as columns, but given the gpos
63-
# function positions subplots from left to right, row by row from the top,
64-
# we need to interleave the values in a list. We can use zip to do this.
65-
#
56+
# %%
57+
# 4. We plot each category of colourmap in a given columns of the subplot,
58+
# but given the 'gpos' function positions subplots from left to right, row by
59+
# row from the top, we need to interleave the values in a list. We can use
60+
# zip to do this:
6661
colour_scales_columns = [
67-
val
68-
for category in zip(colour_scale_pu, colour_scale_ncl, colour_scale_ob)
69-
for val in category
62+
cscale
63+
for category in zip(colour_scales_pu, colour_scales_ncl, colour_scales_ob)
64+
for cscale in category
7065
]
7166

72-
zip(colour_scale_pu, colour_scale_ncl, colour_scale_ob)
67+
7368
# %%
74-
# 4. We then use a for loop to cycle through all the different colour maps:
75-
# Only gpos has 1 added because it can only take 1 as its first value,
76-
# otherwise there are errors.
77-
cfp.gopen(rows=3, columns=3, bottom=0.1, top=0.85, file="ColourPlot.png")
69+
# 5. Create the figure and give it an overall title. Ensure the
70+
# number of rows * number of columns = number of colour scales:
71+
cfp.gopen(rows=3, columns=3, bottom=0.1, top=0.85, file="colour_scales.png")
7872
plt.suptitle(
7973
(
8074
"Air temperature (K) at 850 mbar pressure shown in different "
81-
"colourmap categories"
75+
"categories of colour scale"
8276
),
8377
fontsize=18,
8478
)
79+
80+
# %%
81+
# 6. We loop through all the different colour maps defined and plot
82+
# as subplots, with each category in the same column, labelling each column
83+
# with the colour scale category:
8584
for i, colour_scale in enumerate(colour_scales_columns):
8685
cfp.gpos(i + 1)
8786
cfp.cscale(colour_scale)
88-
if i == len(colour_scale) + 1:
89-
# For the final plot, don't plot the colourbar across all subplots
90-
# as is the default
91-
cfp.con(
92-
f.subspace(pressure=850),
93-
lines=False,
94-
axes=False,
95-
colorbar_drawedges=False,
96-
colorbar_title=f"Shown in '{colour_scale}'",
97-
colorbar_fraction=0.04,
98-
colorbar_thick=0.02,
99-
colorbar_fontsize=11,
100-
)
101-
elif i < 3:
87+
88+
# For the topmost plots, label the coumn with the colour scale category
89+
# using the 'title' argument, otherwise don't add a title.
90+
if i < 3:
91+
# Ensure the order the titles are written in corresponds to the
92+
# order unzipped in step 4, so the columns match up correctly.
10293
if i == 0:
10394
set_title = "Perceptually uniform\ncolour maps"
10495
elif i == 1:
@@ -110,16 +101,15 @@
110101

111102
cfp.con(
112103
f.subspace(pressure=850),
104+
title=set_title,
113105
lines=False,
114106
axes=False,
115-
title=set_title,
116107
colorbar_drawedges=False,
117108
colorbar_title=f"Shown in '{colour_scale}'",
118109
colorbar_fraction=0.04,
119110
colorbar_thick=0.02,
120111
colorbar_fontsize=11,
121112
)
122-
123113
else:
124114
cfp.con(
125115
f.subspace(pressure=850),

0 commit comments

Comments
 (0)