Skip to content

Commit 9836d52

Browse files
Merge pull request #819 from sadielbartholomew/student-recipes-2
Add new recipe (17) by summer student: colourmap subplots
2 parents ac9ce15 + e0eeba5 commit 9836d52

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
"""
2+
Plotting contour subplots with different colour maps/scales
3+
===========================================================
4+
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.
9+
"""
10+
11+
# %%
12+
# 1. Import cf-python and cf-plot:
13+
14+
import cfplot as cfp
15+
import matplotlib.pyplot as plt
16+
17+
import cf
18+
19+
# %%
20+
# 2. Read the field in:
21+
f = cf.read("~/recipes/ggap.nc")[0]
22+
23+
# %%
24+
# 3. Choose a set of predefined colour scales to view. These can be chosen
25+
# from the selection at:
26+
# https://ncas-cms.github.io/cf-plot/build/colour_scales.html or you
27+
# can define your own, see:
28+
# https://ncas-cms.github.io/cf-plot/build/colour_scales.html#user-defined-colour-scales.
29+
# Here we take three colour scales each from three different general
30+
# categories, to showcase some differences in representation.
31+
# Note colour scale levels can be adjusted using 'cscale' and keywords such as:
32+
# cfp.cscale(<cmap name>, ncols=16, below=2, above=14)
33+
34+
# %%
35+
# a. Perceptually uniform colour scales, with no zero value, see:
36+
# https://ncas-cms.github.io/cf-plot/build/colour_scales.html#perceptually-uniform-colour-scales.
37+
colour_scales_pu = ["viridis", "magma", "plasma"]
38+
39+
# %%
40+
# b. NCAR Command Language colour scales enhanced to help with colour
41+
# blindness, see:
42+
# https://ncas-cms.github.io/cf-plot/build/colour_scales.html#ncar-command-language-enhanced-to-help-with-colour-blindness.
43+
# These colour maps are better for accessibility.
44+
colour_scales_ncl = ["posneg_1", "GreenMagenta16", "StepSeq25"]
45+
46+
# %%
47+
# c. Orography/bathymetry colour scales, see:
48+
# https://ncas-cms.github.io/cf-plot/build/colour_scales.html#orography-bathymetry-colour-scales.
49+
# These are used to show the shape/contour of land masses, but bear in mind the
50+
# data we show here is air temperature so doesn't represent this and
51+
# therefore it is not a good choice in this case:
52+
colour_scales_ob = ["wiki_1_0_2", "wiki_2_0", "wiki_2_0_reduced"]
53+
54+
55+
# %%
56+
# 4. We plot each category of colourmap in a given columns of the subplot,
57+
# but given the 'gpos' function positions subplots from left to right, row by
58+
# row from the top, we need to interleave the values in a list. We can use
59+
# zip to do this:
60+
colour_scales_columns = [
61+
cscale
62+
for category in zip(colour_scales_pu, colour_scales_ncl, colour_scales_ob)
63+
for cscale in category
64+
]
65+
66+
67+
# %%
68+
# 5. Create the figure and give it an overall title. Ensure the
69+
# number of rows * number of columns = number of colour scales:
70+
cfp.gopen(rows=3, columns=3, bottom=0.1, top=0.85)
71+
plt.suptitle(
72+
(
73+
"Air temperature (K) at 850 mbar pressure shown in different "
74+
"categories of colour scale"
75+
),
76+
fontsize=18,
77+
)
78+
79+
# %%
80+
# 6. We loop through all the different colour maps defined and plot
81+
# as subplots, with each category in the same column, labelling each column
82+
# with the colour scale category:
83+
for i, colour_scale in enumerate(colour_scales_columns):
84+
cfp.gpos(i + 1)
85+
cfp.cscale(colour_scale)
86+
87+
# For the topmost plots, label the column with the colour scale category
88+
# using the 'title' argument, otherwise don't add a title.
89+
# Ensure the order the titles are written in corresponds to the
90+
# order unzipped in step 4, so the columns match up correctly.
91+
if i == 0:
92+
set_title = "Perceptually uniform\ncolour maps"
93+
elif i == 1:
94+
set_title = (
95+
"NCL colour maps enhanced to \nhelp with colour blindness"
96+
)
97+
elif i == 2:
98+
set_title = "Orography/bathymetry\ncolour maps"
99+
else:
100+
set_title = ""
101+
102+
cfp.con(
103+
f.subspace(pressure=850),
104+
title=set_title,
105+
lines=False,
106+
axes=False,
107+
colorbar_drawedges=False,
108+
colorbar_title=f"Shown in '{colour_scale}'",
109+
colorbar_fraction=0.04,
110+
colorbar_thick=0.02,
111+
colorbar_fontsize=11,
112+
)
113+
114+
cfp.gclose(view=True)

docs/source/recipes/recipe_list.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ plot_14_recipe.html#sphx-glr-recipes-plot-14-recipe-py
2929
plot_15_recipe.html#sphx-glr-recipes-plot-15-recipe-py
3030
<div class="sphx-glr-thumbcontainer histogram subspace" tooltip="Histogram, Subspace, Subplot">
3131
plot_16_recipe.html#sphx-glr-recipes-plot-16-recipe-py
32+
<div class="sphx-glr-thumbcontainer histogram subspace" tooltip="Histogram, Subspace">
33+
plot_17_recipe.html#sphx-glr-recipes-plot-17-recipe-py
3234
<div class="sphx-glr-thumbcontainer contourmap subspace subplot" tooltip="Contourmap, Subspace, Subplot">

0 commit comments

Comments
 (0)