Examples#

Below is a gallery showcasing particular examples introducing a type/theme category, but to browse all examples please consult the full listing.

Listing of all examples#

Note

All example code assumes the following imports have been made, where numpy is usually not required but it is for some of the examples:

Imports required as setup for the examples, noting that cfplot is always aliased to cfp by convention#
import cfplot as cfp
import cf
import numpy as np  # only required for some examples

Tip

Most of the examples use cf-python to read in a dataset to a FieldList and then extract one or more Field objects from the resulting FieldList to use (for plotting), which can be done either by indexing or through use of the cf-python selection methods, select_by_*.

For consistency across the examples we use the select_by_identity method to extract field(s) because it is more explicit about the nature of the Field choice than indexing, except where the FieldList contains only one field where we use the first (0th) index to take the only field. But note the alternative approaches:

Some alternative ways to extract a Field from a FieldList using cf-python#
>>> # Get the FieldList (call it 'fl')
>>> fl = cf.read(f"cfplot_data/ggap.nc")
>>> print(fl)
[<CF Field: divergence_of_wind(time(1), pressure(23), latitude(160), longitude(320)) s**-1>,
 <CF Field: long_name=Ozone mass mixing ratio(time(1), pressure(23), latitude(160), longitude(320)) kg kg**-1>,
 <CF Field: long_name=Potential vorticity(time(1), pressure(23), latitude(160), longitude(320)) K m**2 kg**-1 s**-1>,
 <CF Field: specific_humidity(time(1), pressure(23), latitude(160), longitude(320)) kg kg**-1>,
 ...
]

>>> # Extract the fourth field by indexing
>>> f = fl[3]
>>> f
<CF Field: specific_humidity(time(1), pressure(23), latitude(160), longitude(320)) kg kg**-1>

>>> # Extract the same field through select_by_identity
>>> g = fl.select_by_identity("specific_humidity")[0]
>>> # Check this is the same as the f field above
>>> g.equals(f)
True

>>> # Extract the same field through select_by_units (and indexing)
>>> fl.select_by_units("kg kg**-1")
[<CF Field: long_name=Ozone mass mixing ratio(time(1), pressure(23), latitude(160), longitude(320)) kg kg**-1>,
 <CF Field: specific_humidity(time(1), pressure(23), latitude(160), longitude(320)) kg kg**-1>]
>>> h = fl.select_by_units("kg kg**-1")[1]
>>> # Check this is the same as the f field above (and hence g too)
>>> h.equals(f)

Example Datasets#

The examples make use of a small number of sample datasets. These are available to view and download from a dedicated directory in the codebase repository. Alternatively, they may be downloaded together as a pair of zip files (split into two parts to fit within GitHub file size limits, total size ~125 MB) available from here (part 1 of 2) and here (part 2 of 2) which can be unzipped together to the full dataset directory as follows:

Accessing all of the datasets for the cf-plot examples#
# Download the split zip parts: either use 'wget' as below or
# download both files from the links above
wget https://github.com/NCAS-CMS/cf-plot/tree/main/docs/source/_downloads/example-datasets.z01
wget https://github.com/NCAS-CMS/cf-plot/tree/main/docs/source/_downloads/example-datasets.zip

# Recombine into a single zip (zip -s0 merges split parts)
zip -s0 example-datasets.zip --out all-example-datasets.zip

# Unzip the merged file
unzip all-example-datasets.zip