|
2 | 2 | Plotting contour subplots with different colour maps/scales
|
3 | 3 | ===========================================================
|
4 | 4 |
|
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. |
9 | 9 | """
|
10 | 10 |
|
11 | 11 | # %%
|
|
18 | 18 |
|
19 | 19 | # %%
|
20 | 20 | # 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) |
23 | 21 | PATH = "~/git-repos/cf-plot/cfplot/test/cfplot_data"
|
24 | 22 | f = cf.read(f"{PATH}/ggap.nc")[0]
|
25 | 23 |
|
26 | 24 | # %%
|
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) |
34 | 34 |
|
35 | 35 | # %%
|
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"] |
39 | 39 |
|
40 | 40 | # %%
|
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"] |
48 | 46 |
|
49 | 47 | # %%
|
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"] |
60 | 54 |
|
61 | 55 |
|
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: |
66 | 61 | 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 |
70 | 65 | ]
|
71 | 66 |
|
72 |
| -zip(colour_scale_pu, colour_scale_ncl, colour_scale_ob) |
| 67 | + |
73 | 68 | # %%
|
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") |
78 | 72 | plt.suptitle(
|
79 | 73 | (
|
80 | 74 | "Air temperature (K) at 850 mbar pressure shown in different "
|
81 |
| - "colourmap categories" |
| 75 | + "categories of colour scale" |
82 | 76 | ),
|
83 | 77 | fontsize=18,
|
84 | 78 | )
|
| 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: |
85 | 84 | for i, colour_scale in enumerate(colour_scales_columns):
|
86 | 85 | cfp.gpos(i + 1)
|
87 | 86 | 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. |
102 | 93 | if i == 0:
|
103 | 94 | set_title = "Perceptually uniform\ncolour maps"
|
104 | 95 | elif i == 1:
|
|
110 | 101 |
|
111 | 102 | cfp.con(
|
112 | 103 | f.subspace(pressure=850),
|
| 104 | + title=set_title, |
113 | 105 | lines=False,
|
114 | 106 | axes=False,
|
115 |
| - title=set_title, |
116 | 107 | colorbar_drawedges=False,
|
117 | 108 | colorbar_title=f"Shown in '{colour_scale}'",
|
118 | 109 | colorbar_fraction=0.04,
|
119 | 110 | colorbar_thick=0.02,
|
120 | 111 | colorbar_fontsize=11,
|
121 | 112 | )
|
122 |
| - |
123 | 113 | else:
|
124 | 114 | cfp.con(
|
125 | 115 | f.subspace(pressure=850),
|
|
0 commit comments