Skip to content

Commit

Permalink
Merge pull request #410 from NCAR/native1_with_set_gridlines
Browse files Browse the repository at this point in the history
add_lat_lon_gridlines in native_1 and native_2
  • Loading branch information
pilotchute committed Dec 13, 2022
2 parents 9947497 + 7f8266c commit bb72b3f
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 6 deletions.
56 changes: 56 additions & 0 deletions Gallery/MapProjections/NCL_native_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,53 @@
import geocat.viz as gv
import geocat.datafiles as gdf

###############################################################################
# Potential viz covenience function add_lat_lon_gridlines


def add_lat_lon_gridlines(ax,
projection=None,
draw_labels=True,
xlocator=np.arange(-180, 180, 15),
ylocator=np.arange(-90, 90, 15),
labelsize=12,
**kwargs):
"""Utility function that adds latitude and longtitude gridlines to the
plot.
Args:
ax (:class:`cartopy.mpl.geoaxes.GeoAxes`):
Current axes to the current figure.
projection (:class:`cartopy.crs.CRS`):
Defines a Cartopy Coordinate Reference System. If not given,
defaults to ccrs.PlateCarree()
draw_labels (:class:`bool`):
Toggle whether to draw labels, default to True.
xlocator, ylocator (:class:`numpy.ndarray` or list):
Arrays of fixed locations of the gridlines in the x and y coordinate of the given CRS.
Default to np.arange(-180, 180, 15) and np.arange(-90, 90, 15).
labelsize (:class:`float`):
Fontsizes of label fontsizes of x and y coordinates.
*kwargs* control line properties and are passed through to `matplotlib.collections.Collection`.
Return:
gl (:class:`cartopy.mpl.gridliner.Gridliner`):
"""
import matplotlib.ticker as mticker

# Draw gridlines
gl = ax.gridlines(crs=projection,
draw_labels=draw_labels,
x_inline=False,
y_inline=False,
**kwargs)

gl.xlocator = mticker.FixedLocator(xlocator)
gl.ylocator = mticker.FixedLocator(ylocator)
gl.xlabel_style = {"rotation": 0, "size": labelsize}
gl.ylabel_style = {"rotation": 0, "size": labelsize}

return gl


###############################################################################
# Read in data:
nlat = 293
Expand Down Expand Up @@ -57,6 +104,15 @@
# and 42.25N to 49.25N
ax.set_extent([4.25, 15.25, 42.25, 49.25], ccrs.PlateCarree())

# Draw gridlines
add_lat_lon_gridlines(ax,
xlocator=np.arange(4, 18, 2),
ylocator=np.arange(43, 50),
labelsize=14,
linewidth=1,
color='black',
alpha=0.25)

# 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
60 changes: 54 additions & 6 deletions Gallery/MapProjections/NCL_native_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,53 @@
decode_times=False)
t = ds.FSD.isel(time=0)

###############################################################################
# Potential viz covenience function add_lat_lon_gridlines


def add_lat_lon_gridlines(ax,
projection=None,
draw_labels=True,
xlocator=np.arange(-180, 180, 15),
ylocator=np.arange(-90, 90, 15),
labelsize=12,
**kwargs):
"""Utility function that adds latitude and longtitude gridlines to the
plot.
Args:
ax (:class:`cartopy.mpl.geoaxes.GeoAxes`):
Current axes to the current figure.
projection (:class:`cartopy.crs.CRS`):
Defines a Cartopy Coordinate Reference System. If not given,
defaults to ccrs.PlateCarree()
draw_labels (:class:`bool`):
Toggle whether to draw labels, default to True.
xlocator, ylocator (:class:`numpy.ndarray` or list):
Arrays of fixed locations of the gridlines in the x and y coordinate of the given CRS.
Default to np.arange(-180, 180, 15) and np.arange(-90, 90, 15).
labelsize (:class:`float`):
Fontsizes of label fontsizes of x and y coordinates.
*kwargs* control line properties and are passed through to `matplotlib.collections.Collection`.
Return:
gl (:class:`cartopy.mpl.gridliner.Gridliner`):
"""
import matplotlib.ticker as mticker

# Draw gridlines
gl = ax.gridlines(crs=projection,
draw_labels=draw_labels,
x_inline=False,
y_inline=False,
**kwargs)

gl.xlocator = mticker.FixedLocator(xlocator)
gl.ylocator = mticker.FixedLocator(ylocator)
gl.xlabel_style = {"rotation": 0, "size": labelsize}
gl.ylabel_style = {"rotation": 0, "size": labelsize}

return gl


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

Expand Down Expand Up @@ -68,12 +115,13 @@
ticks=cbar_ticks)

# Draw gridlines
gl = gv.add_lat_lon_gridlines(
ax,
color='grey',
labelsize=14,
xlocator=np.arange(130, 144, 4), # longitudes for gridlines
ylocator=np.arange(36, 51, 2)) # latitudes for gridlines
gl = add_lat_lon_gridlines(ax,
xlocator=[130, 134, 138, 142],
ylocator=[36, 38, 40, 42, 44, 46, 48, 50],
labelsize=15,
linewidth=1,
color='black',
alpha=0.25)

# Remove lat/lon labels on top and right sides of plot
gl.top_labels = False
Expand Down

0 comments on commit bb72b3f

Please sign in to comment.