This script illustrates the following concepts:
  • Plotting a simple trajectory plot

  • Plotting multiple trajectories in different colors

  • Plotting every nth time step in a trajectory

See following URLs to see the reproduced NCL plot & script:

Import packages:

import xarray as xr
import geocat.datafiles as gdf
import as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import numpy as np

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/'))
sdata = ds.get('sdata')

Define helper function to plot every n-th timestep:

def plot_nth_timestep(nparrayy, nparrayx, n):

    for x in range(0, len(nparrayx)):

        # Plot green starting point of each trajectory
        if x == 0:
            y, x = nparrayy[x], nparrayx[x]
            plt.scatter(x, y, color='green', s=1, zorder=2.5)

        # Plot every n-th timestamp
        if x % n == 0:
            y, x = nparrayy[x], nparrayx[x]
            plt.scatter(x, y, color='black', s=1, zorder=2.5)


# Initialize axes
ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_extent([-75, -25, -60, -20], crs=None)

# Set title and subtitle
plt.suptitle('Example of a Trajectory Plot')
plt.title('markers every 4th timestep', fontsize=10, pad=10)

# Set land feature and change color to 'lightgrey'
# See link for extensive list of colors:
ax.add_feature(cfeature.LAND, color='lightgrey')

# Use geocat.viz.util convenience function to make plots look like NCL plots by using latitude, longitude tick labels

# Use geocat.viz.util convenience function to set axes tick values
                             xticks=np.linspace(-70, -30, 5),
                             yticks=np.linspace(-60, -20, 5))

# Select trajectories to plot
traj = [1, 10, 53, 67, 80]

# Set colors of each trajectory line
trajlinecolors = ["red", "blue", "green", "grey", "magenta"]

# Plot each trajectory
for i in range(len(traj)):

    # Extract latitude
    ypt = (np.array(sdata[1, :, traj[i]]) - 360)

    # Extract longitude
    xpt = np.array(sdata[2, :, traj[i]])

    plt.plot(ypt, xpt, color=trajlinecolors[i], linewidth=0.4)

    plot_nth_timestep(xpt, ypt, n=4)
Example of a Trajectory Plot, markers every 4th timestep

Total running time of the script: (0 minutes 0.998 seconds)

Gallery generated by Sphinx-Gallery