Skip to content

Commit

Permalink
Merge pull request #441 from NCAR/vector_1
Browse files Browse the repository at this point in the history
Vector 1
  • Loading branch information
anissa111 committed Jul 15, 2022
2 parents 6e5e4be + 48c8010 commit 4a9878e
Showing 1 changed file with 102 additions and 84 deletions.
186 changes: 102 additions & 84 deletions Gallery/Vectors/NCL_vector_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
===============
Plot U & V vector over SST
Note: The colormap on this plot has been changed from the original NCL colormap
in order to follow best practices for colormaps. See other examples here:
https://geocat-examples.readthedocs.io/en/latest/gallery/index.html#colors
This script illustrates the following concepts:
- Overlaying vectors and filled contours on a map
- Changing the scale of the vectors on the plot
Expand All @@ -18,12 +23,13 @@
###############################################################################
# Import packages

import xarray as xr
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from matplotlib import pyplot as plt
import cartopy
import xarray as xr
import cartopy.feature as cfeature
import cartopy.crs as ccrs
import cmaps
from cartopy.mpl.gridliner import LongitudeFormatter, LatitudeFormatter

import geocat.datafiles as gdf
import geocat.viz as gv
Expand All @@ -47,105 +53,117 @@
v = uv_in['V'].sel(date=198801, lev=1000)

# Read in grid information
lat_sst = sst['lat']
lon_sst = sst['lon']
lat_uv = u['lat']
lon_uv = u['lon']

###############################################################################
# Plot:

# Define map projection to use
proj = ccrs.PlateCarree()

# Generate figure (set its size (width, height) in inches)
plt.figure(figsize=(10, 7))

# Generate axes using Cartopy projection
ax = plt.axes(projection=ccrs.PlateCarree())

# Draw vector plot
Q = plt.quiver(lon_uv,
lat_uv,
u,
v,
color='white',
pivot='middle',
width=.0025,
scale=75,
zorder=2)

# Turn on continent shading
ax.add_feature(cartopy.feature.LAND,
edgecolor='lightgray',
facecolor='lightgray',
zorder=1)

# Define levels for contour map (24, 24.1, ..., 28.8, 28.9)
levels = np.linspace(24, 28.9, 50)

# Import an NCL colormap, truncating it by using geocat.viz.util convenience function
gv.truncate_colormap(cmaps.BlAqGrYeOrReVi200,
minval=0.08,
maxval=0.96,
n=len(levels),
name='BlAqGrYeOrReVi200')

# Contourf-plot the SST data
cf = sst.plot.contourf('lon',
'lat',
extend='both',
levels=levels,
cmap='BlAqGrYeOrReVi200',
zorder=0,
add_labels=False,
add_colorbar=False)

# Add color bar
cbar_ticks = np.arange(24, 29.1, .3)
cbar = plt.colorbar(cf,
orientation='vertical',
drawedges=True,
shrink=0.75,
pad=0.05,
ticks=cbar_ticks)

# Draw the key for the quiver plot as a rectangle patch
rect = plt.Rectangle((92.9, 22.6),
2,
2,
facecolor='white',
edgecolor=None,
zorder=2)
ax.add_patch(rect)
ax.quiverkey(Q,
0.9675,
0.9,
3,
'4',
labelpos='N',
color='black',
coordinates='axes',
fontproperties={'size': 14},
labelsep=0.1)

# Use geocat.viz.util convenience function to set axes tick values
# Define axis using Cartopy and zoom in on the region of interest
ax = plt.axes(projection=proj)
ax.set_extent((66, 96, 5, 25), crs=ccrs.PlateCarree())

# Create the filled contour plot
sst_plot = sst.plot.contourf(
ax=ax,
transform=proj,
levels=50,
vmin=24,
vmax=28.8,
cmap="magma",
add_colorbar=False,
)

# Remove default x and y labels from plot
plt.xlabel("")
plt.ylabel("")

# add land feature
ax.add_feature(cfeature.LAND, facecolor="lightgrey", zorder=1)

# Add vectors onto the plot
Q = plt.quiver(
lon_uv,
lat_uv,
u,
v,
color='white',
pivot='middle',
width=.0025,
scale=75,
)

# Use geocat-viz utility function to format title
gv.set_titles_and_labels(ax,
maintitle='',
maintitlefontsize=18,
lefttitle="Sea Surface Temperature",
lefttitlefontsize=18,
righttitle="C",
righttitlefontsize=18,
xlabel=None,
ylabel=None,
labelfontsize=16)

# Format tick labels as latitude and longitudes
gv.add_lat_lon_ticklabels(ax=ax)

# Use geocat-viz utility function to customize tick marks
gv.set_axes_limits_and_ticks(ax,
xlim=(65, 95),
ylim=(5, 25),
xticks=range(70, 95, 10),
yticks=range(5, 27, 5))

# Use geocat.viz.util convenience function to add minor and major tick lines
# Remove degree symbol from tick labels
ax.yaxis.set_major_formatter(LatitudeFormatter(degree_symbol=''))
ax.xaxis.set_major_formatter(LongitudeFormatter(degree_symbol=''))

# Add minor tick marks
gv.add_major_minor_ticks(ax,
x_minor_per_major=5,
y_minor_per_major=5,
x_minor_per_major=4,
y_minor_per_major=4,
labelsize=14)

# Use geocat.viz.util convenience function to make plots look like NCL plots by using latitude, longitude tick labels
gv.add_lat_lon_ticklabels(ax)
# Draw the key for the quiver plot as a rectangle patch
rect = mpl.patches.Rectangle(
(91.7, 22.7), # (x, y)
3.2, # width
2.2, # height
facecolor='white',
edgecolor='k',
)
ax.add_patch(rect)

# Use geocat.viz.util convenience function to add titles to left and right of the plot axis.
gv.set_titles_and_labels(ax,
lefttitle='Sea Surface Temperature',
righttitle='C')
qk = ax.quiverkey(
Q, # the quiver instance
0.95, # x position of the key
0.9, # y position of the key
4, # length of the key
'4', # label for the key
labelpos='N', # position the label to the 'north' of the arrow
color='black',
coordinates='axes',
fontproperties={'size': 14},
labelsep=0.1, # Distance between arrow and label
)

# Add and customize colorbar
cbar_ticks = np.arange(24, 28.8, .3)
plt.colorbar(ax=ax,
mappable=sst_plot,
extendrect=True,
extendfrac='auto',
shrink=0.75,
aspect=10,
ticks=cbar_ticks,
drawedges=True)

# Show the plot
plt.show()

0 comments on commit 4a9878e

Please sign in to comment.