Skip to content

Commit

Permalink
Merge branch 'main' into panel_4
Browse files Browse the repository at this point in the history
  • Loading branch information
Heather Craker committed Jul 15, 2022
2 parents 0151126 + 7d76b2c commit f80d112
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 108 deletions.
38 changes: 14 additions & 24 deletions Gallery/MapProjections/NCL_native_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import numpy as np
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import cmaps

import geocat.viz as gv
Expand Down Expand Up @@ -58,22 +57,6 @@
# and 42.25N to 49.25N
ax.set_extent([4.25, 15.25, 42.25, 49.25], ccrs.PlateCarree())

# Draw gridlines
gl = ax.gridlines(crs=ccrs.PlateCarree(),
draw_labels=True,
dms=False,
x_inline=False,
y_inline=False,
linewidth=1,
color="black",
alpha=0.25)

# Manipulate latitude and longitude gridline numbers and spacing
gl.xlocator = mticker.FixedLocator(np.arange(4, 18, 2))
gl.ylocator = mticker.FixedLocator(np.arange(43, 50))
gl.xlabel_style = {"rotation": 0, "size": 14}
gl.ylabel_style = {"rotation": 0, "size": 14}

# Create colormap by choosing colors from existing colormap
# The brightness of the colors in cmocean_speed increase linearly. This
# makes the colormap easier to interpret for those with vision impairments
Expand Down Expand Up @@ -106,19 +89,26 @@
pad=0.1,
shrink=0.8)

# Use geocat-viz utility function to add gridlines to the map
gl = gv.add_lat_lon_gridlines(
ax,
color='black',
labelsize=14,
xlocator=np.arange(4, 18, 2), # longitudes for gridlines
ylocator=np.arange(43, 50)) # latitudes for gridlines

# Add padding between figure and longitude labels
gl.xpadding = 12

# Use geocat.viz.util function to easily set left and right titles
gv.set_titles_and_labels(ax,
lefttitle="topography",
lefttitlefontsize=14,
lefttitlefontsize=16,
righttitle="m",
righttitlefontsize=14)
righttitlefontsize=16)

# Add a main title above the left and right titles
plt.title("Native Sterographic Example",
loc="center",
y=1.1,
size=18,
fontweight="bold")
plt.title("Native Stereographic Example", y=1.1, size=18, fontweight="bold")

# Remove whitespace around plot
plt.tight_layout()
Expand Down
78 changes: 78 additions & 0 deletions Gallery/TaylorDiagrams/NCL_taylor_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
NCL_taylor_2.py
===============
Concepts illustrated:
- Creating a simple Taylor Diagram
- Adding background options for the diagram
See following URLs to see the reproduced NCL plot & script:
- Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/taylor_2.ncl
- Original NCL plot: https://www.ncl.ucar.edu/Applications/Images/taylor_2_lg.png
Note: Due to to limitations of matplotlib's axisartist toolkit, we cannot include minor tick marks
between 0.9 and 0.99, as seen in the original NCL plot.
"""

###############################################################################
# Import packages:

import matplotlib.pyplot as plt
import numpy as np

import geocat.viz as gv

###############################################################################
# Create dummy data:

# p dataset
pstddev = [0.6, 0.5, 0.45, 0.75, 1.15] # standard deviation
pcorrcoef = [0.24, 0.75, 1, 0.93, 0.37] # correlation coefficient

# t dataset
tstddev = [0.75, 0.64, 0.4, 0.85, 1.15]
tcorrcoef = [0.24, 0.75, 0.47, 0.88, 0.73]

###############################################################################
# Plot

# Create figure and Taylor Diagram instance
fig = plt.figure(figsize=(12, 12))
dia = gv.TaylorDiagram(fig=fig, label='REF')
ax = plt.gca()

# Add model sets for p and t datasets
dia.add_model_set(
pstddev,
pcorrcoef,
fontsize=20,
xytext=(-5, 10), # marker label location, in pixels
color='red',
marker='o',
facecolors='none',
s=100) # marker size
dia.add_model_set(
tstddev,
tcorrcoef,
fontsize=20,
xytext=(-5, 10), # marker label location, in pixels
color='blue',
marker='D',
facecolors='none',
s=100)

# Add RMS contours, and label them
dia.add_contours(levels=np.arange(0, 1.1, 0.25),
colors='lightgrey',
linewidths=0.5)

# Add y axis grid
dia.add_ygrid(np.array([0.5, 1.5]), color='grey')

# Add x axis grid
dia.add_xgrid(np.array([0.6, 0.9]))

# Add figure title
plt.title("Example", size=26, pad=45)

# Show the plot
plt.show()
6 changes: 6 additions & 0 deletions Gallery/TaylorDiagrams/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. _taylor_diagrams_examples:

.. _taylor-diagrams-index:

Taylor Diagrams
===============
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 f80d112

Please sign in to comment.