|
12 | 12 | # 1. Import cf-python and cf-plot:
|
13 | 13 |
|
14 | 14 | import cfplot as cfp
|
| 15 | +import matplotlib.pyplot as plt |
15 | 16 |
|
16 | 17 | import cf
|
17 | 18 |
|
18 | 19 | # %%
|
19 | 20 | # 2. Read the field in:
|
20 | 21 | # Here I've used sample data ggap.nc (and later pressure=850), but you
|
21 | 22 | # 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" |
23 | 24 | f = cf.read(f"{PATH}/ggap.nc")[0]
|
24 | 25 |
|
25 | 26 | # %%
|
|
30 | 31 | # Simply change the name in quotes and ensure the
|
31 | 32 | # number of rows * number of columns = number of colour scales
|
32 | 33 |
|
| 34 | + |
33 | 35 | # %%
|
34 | 36 | # 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 | + |
37 | 43 |
|
38 | 44 | # %%
|
39 | 45 | # b. NCAR Command Language - Enhanced to help with colour blindness
|
40 |
| -colour_scale = [ |
| 46 | +colour_scale_ncl = [ |
41 | 47 | "StepSeq25",
|
42 | 48 | "posneg_2",
|
43 |
| - "posneg_1", |
44 |
| - "BlueDarkOrange18", |
45 |
| - "BlueDarkRed18", |
| 49 | + # "posneg_1", |
| 50 | + # "BlueDarkOrange18", |
| 51 | + # "BlueDarkRed18", |
46 | 52 | "GreenMagenta16",
|
47 |
| - "BlueGreen14", |
48 |
| - "BrownBlue12", |
49 |
| - "Cat12", |
| 53 | + # "BlueGreen14", |
| 54 | + # "BrownBlue12", |
| 55 | + # "Cat12", |
50 | 56 | ]
|
51 |
| -cfp.gopen(rows=3, columns=3, bottom=0.1) |
| 57 | + |
52 | 58 |
|
53 | 59 | # %%
|
54 | 60 | # c. Orography/bathymetry colour scales
|
55 | 61 | # These are used to show the shape/contour of landmasses, bear in mind the
|
56 | 62 | # example data we use is with pressure so doesnt accurately represent this.
|
57 | 63 | # You could instead use cfp.cscale('wiki_2_0', ncols=16, below=2, above=14)
|
58 | 64 | # or any other orography colour scale in a similar way.
|
59 |
| -colour_scale = [ |
| 65 | +colour_scale_ob = [ |
60 | 66 | "os250kmetres",
|
61 | 67 | "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", |
65 | 71 | "arctic",
|
66 | 72 | ]
|
67 |
| -cfp.gopen(rows=2, columns=3, bottom=0.2, file="ColourPlot.png") |
68 | 73 |
|
| 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) |
69 | 86 | # %%
|
70 | 87 | # 4. We then use a for loop to cycle through all the different colour maps:
|
71 | 88 | # Only gpos has 1 added because it can only take 1 as its first value,
|
72 | 89 | # 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): |
74 | 99 | cfp.gpos(i + 1)
|
75 |
| - cfp.mapset(proj="cyl") |
76 | 100 | cfp.cscale(colour_scale)
|
77 | 101 | if i == len(colour_scale) + 1:
|
| 102 | + # For the final plot, don't plot the colourbar across all subplots |
| 103 | + # as is the default |
78 | 104 | cfp.con(
|
79 | 105 | f.subspace(pressure=850),
|
80 | 106 | 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, |
84 | 112 | )
|
| 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 | + |
85 | 134 | 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 | + |
87 | 145 | cfp.gclose(view=True)
|
0 commit comments