add_height_from_pressure_axis

add_height_from_pressure_axis#

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

# Import Packages:

import numpy as np
import xarray as xr
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter
import cmaps
import metpy.calc as mpcalc
from metpy.units import units

import scipy

import geocat.datafiles as gdf
import geocat.viz as gv
Downloading file 'registry.txt' from 'https://github.com/NCAR/GeoCAT-datafiles/raw/main/registry.txt' to '/home/docs/.cache/geocat'.
# Open a netCDF data file using xarray default engine and load the data into xarrays
ds = xr.open_dataset(gdf.get("netcdf_files/mxclim.nc"))

# Extract variables
U = ds.U[0, :, :]
Downloading file 'netcdf_files/mxclim.nc' from 'https://github.com/NCAR/GeoCAT-datafiles/raw/main/netcdf_files/mxclim.nc' to '/home/docs/.cache/geocat'.
# Generate figure (set its size (width, height) in inches) and axes
plt.figure(figsize=(8, 8))
ax = plt.axes()

# Set y-axis to have log-scale
plt.yscale('log')

# Specify which contours should be drawn
levels = np.linspace(-55, 55, 23)

# Plot contour lines
lines = U.plot.contour(ax=ax,
                       levels=levels,
                       colors='black',
                       linewidths=0.5,
                       linestyles='solid',
                       add_labels=False)

# Invert y-axis
ax.invert_yaxis()

# Create second y-axis to show geo-potential height.
axRHS = gv.add_height_from_pressure_axis(ax, heights=[4, 8])

plt.show();
../_images/5e077a79495c81edaec16c89c50dcc3acce1328c918d4f3d5d7176f5148510d7.png