|
| 1 | +""" |
| 2 | +Plotting contour subplots with different projections |
| 3 | +==================================================== |
| 4 | +
|
| 5 | +In this recipe, we will plot the same data using different projections |
| 6 | +as subplots to illustrate visually some available possibilities. |
| 7 | +
|
| 8 | +""" |
| 9 | + |
| 10 | +# %% |
| 11 | +# 1. Import cf-python and cf-plot: |
| 12 | + |
| 13 | +import cfplot as cfp |
| 14 | + |
| 15 | +import cf |
| 16 | + |
| 17 | +# %% |
| 18 | +# 2. Read the field in: |
| 19 | +f = cf.read("~/recipes/ggap.nc")[0] |
| 20 | + |
| 21 | +# %% |
| 22 | +# 3. Create the file with subplots. If changing the number of subplots, |
| 23 | +# ensure the number of rows * number of columns = the number of projections. |
| 24 | +# Here we are doing 6 projections so 2 x 3 is fine: |
| 25 | +cfp.gopen(rows=2, columns=3, bottom=0.2, file="projections.png") |
| 26 | + |
| 27 | +# %% |
| 28 | +# 4. List the projection types to use. Here we are using |
| 29 | +# Cylindrical/Default, North Pole Stereographic, South Pole Stereographic, |
| 30 | +# Mollweide, Mercator and Robinson. However there are several other choices |
| 31 | +# possible, see: |
| 32 | +# https://ncas-cms.github.io/cf-plot/build/user_guide.html#appendixc. Our |
| 33 | +# chosen list is: |
| 34 | +projtypes = ["cyl", "npstere", "spstere", "moll", "merc", "robin"] |
| 35 | + |
| 36 | +# %% |
| 37 | +# 5. Loop through the list of projection types and plot each as a sub-plot: |
| 38 | +for i, proj in enumerate(projtypes): |
| 39 | + # gpos has 1 added to the index because it takes 1 as its first value |
| 40 | + cfp.gpos(i + 1) |
| 41 | + cfp.mapset(proj=proj) |
| 42 | + |
| 43 | + # For the final plot only, add a colour bar to cover all the sub-plots |
| 44 | + if i == len(projtypes) - 1: |
| 45 | + cfp.con( |
| 46 | + f.subspace(pressure=850), |
| 47 | + lines=False, |
| 48 | + title=proj, |
| 49 | + colorbar_position=[0.1, 0.1, 0.8, 0.02], |
| 50 | + colorbar_orientation="horizontal", |
| 51 | + ) |
| 52 | + else: |
| 53 | + cfp.con( |
| 54 | + f.subspace(pressure=850), |
| 55 | + lines=False, |
| 56 | + title=proj, |
| 57 | + colorbar=False, |
| 58 | + ) |
| 59 | +cfp.gclose() |
0 commit comments