plot_extrema_labels

plot_extrema_labels#

This notebook is a simple example of the GeoCAT-viz function plot_extrema_labels.

# Import packages:

import xarray as xr
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors
import matplotlib.ticker as mticker

import geocat.datafiles as gdf
import geocat.viz as gv
# Read in data:

# Open a netCDF data file using xarray default engine and
# load the data into xarrays
ds = xr.open_dataset(gdf.get("netcdf_files/slp.1963.nc"), decode_times=False)

# Get data from the 21st timestep
pressure = ds.slp[21, :, :]

# Translate float values to short values
pressure = pressure.astype('float32')

# Convert Pa to hPa data
pressure = pressure * 0.01

# Fix the artifact of not-shown-data around 0 and 360-degree longitudes
wrap_pressure = gv.xr_add_cyclic_longitudes(pressure, "lon")
/home/docs/checkouts/readthedocs.org/user_builds/geocat-viz/conda/stable/lib/python3.12/site-packages/xarray/coding/times.py:170: SerializationWarning: Ambiguous reference date string: 1-1-1 00:00:0.0. The first value is assumed to be the year hence will be padded with zeros to remove the ambiguity (the padded reference date string is: 0001-1-1 00:00:0.0). To remove this message, remove the ambiguity by padding your reference date strings with zeros.
  warnings.warn(warning_msg, SerializationWarning)
# Create plot

# Set figure size
fig = plt.figure(figsize=(8, 8))

# Set global axes with an orthographic projection
proj = ccrs.Orthographic(central_longitude=270, central_latitude=45)
ax = plt.axes(projection=proj)
ax.set_global()

# Plot contour data
p = wrap_pressure.plot.contour(ax=ax,
                               transform=ccrs.PlateCarree(),
                               linewidths=0.3,
                               levels=30,
                               cmap='black',
                               add_labels=False)

# low pressure contour levels- these will be plotted
# as a subscript to an 'L' symbol.
lowClevels = gv.find_local_extrema(pressure, lowVal=995, eType='Low')
highClevels = gv.find_local_extrema(pressure, highVal=1042, eType='High')

# Label low and high contours
gv.plot_extrema_labels(wrap_pressure,
                       ccrs.Geodetic(),
                       proj,
                       label_locations=lowClevels,
                       label='L',
                       show_warnings=False)
gv.plot_extrema_labels(wrap_pressure,
                       ccrs.Geodetic(),
                       proj,
                       label_locations=highClevels,
                       label='H',
                       show_warnings=False)


plt.show();
../_images/862a063515ab51f7e819fa5e0648d285dc7c28ac8c330e6a41e682cb816d4826.png