Skip to content

Commit

Permalink
Merge pull request #501 from jukent/pressure2height
Browse files Browse the repository at this point in the history
update examples to use height-pressure-axis function
  • Loading branch information
jukent committed May 11, 2023
2 parents c10e517 + 784dd9b commit 2aa608b
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 272 deletions.
69 changes: 4 additions & 65 deletions Gallery/Boxplots/NCL_box_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,67 +31,6 @@
for a in range(len(data)):
data[a] = [x - 4 for x in data[a]]

###############################################################################
# Helper function that will be released in GeoCAT-Viz. Delete this block after release.


def set_tick_direction_spine_visibility(ax,
tick_direction='out',
top_spine_visible=True,
bottom_spine_visible=True,
left_spine_visible=True,
right_spine_visible=True):
"""Utility function to turn off axes spines and set tickmark orientations.
Note: This function should be called after calling add_major_minor_ticks()
Args:
ax (:class:`matplotlib.axes._subplots.AxesSubplot` or :class:`cartopy.mpl.geoaxes.GeoAxesSubplot`):
Current axes to the current figure
tick_direction (:class:`str`):
Set 'in' to put ticks inside the axes,
'out' to put ticks outside the axes,
'inout' to put ticks both in and out of the axes.
top_spine_visible (:class:`bool`):
Set False to turn off top spine of the axes.
bottom_spine_visible (:class:`bool`):
Set False to turn off bottom spine of the axes.
left_spine_visible (:class:`bool`):
Set False to turn off left spine of the axes.
right_spine_visible (:class:`bool`):
Set False to turn off right spine.
"""
ax.tick_params(direction=tick_direction, axis='both', which='both')
ax.spines['top'].set_visible(top_spine_visible)
ax.spines['bottom'].set_visible(bottom_spine_visible)
ax.spines['left'].set_visible(left_spine_visible)
ax.spines['right'].set_visible(right_spine_visible)

if top_spine_visible and bottom_spine_visible:
ax.xaxis.set_ticks_position('default')
elif bottom_spine_visible and not top_spine_visible:
ax.xaxis.set_ticks_position('bottom')
elif top_spine_visible and not bottom_spine_visible:
ax.xaxis.set_ticks_position('top')
else:
ax.xaxis.set_ticks_position('none')

if left_spine_visible and right_spine_visible:
ax.yaxis.set_ticks_position('default')
elif not right_spine_visible and left_spine_visible:
ax.yaxis.set_ticks_position('left')
elif not left_spine_visible and right_spine_visible:
ax.yaxis.set_ticks_position('right')
else:
ax.yaxis.set_ticks_position('none')


###############################################################################
# Helper function to set edge color of boxes

Expand Down Expand Up @@ -140,10 +79,10 @@ def setBoxColor(boxplot, colors):
labelsize=16)

# Use geocat.viz.util convenience function to set spines visibility
set_tick_direction_spine_visibility(ax,
tick_direction='in',
top_spine_visible=False,
right_spine_visible=False)
gv.set_tick_direction_spine_visibility(ax,
tick_direction='in',
top_spine_visible=False,
right_spine_visible=False)

# Use geocat.viz.util convenience function to add title to the plot axis.
gv.set_titles_and_labels(ax,
Expand Down
20 changes: 1 addition & 19 deletions Gallery/Contours/NCL_h_lat_6.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,7 @@
labelfontsize=16)

# Create second y-axis to show geo-potential height.
axRHS = ax.twinx()

# Select heights to display as tick labels
heights_nice = np.arange(0, 32, 4)
# Send "nice" height values back to pressure as tick locations
pressures_nice = mpcalc.height_to_pressure_std(heights_nice *
units('km')).magnitude

axRHS.set_yscale('log')
gv.set_axes_limits_and_ticks(axRHS,
ylim=ax.get_ylim(),
yticks=pressures_nice,
yticklabels=heights_nice)

axRHS.tick_params(labelsize=12) # manually set tick label size
axRHS.minorticks_off()

# Use geocat.viz.util convenience function to add titles and the pressure label
gv.set_titles_and_labels(axRHS, ylabel='Height (km)', labelfontsize=16)
axRHS = gv.add_height_from_pressure_axis(ax, heights=[4, 8])

# Force the plot to be square by setting the aspect ratio to 1
ax.set_box_aspect(1)
Expand Down
22 changes: 1 addition & 21 deletions Gallery/Contours/NCL_h_lat_7.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,27 +192,7 @@
labelfontsize=24)

# Create second y-axis to show geo-potential height.
axRHS = ax.twinx()

# Select heights to display as tick labels
heights_nice = np.arange(0, 12, 4)
# Send "nice" height values back to pressure as tick locations
pressures_nice = mpcalc.height_to_pressure_std(heights_nice *
units('km')).magnitude

axRHS.set_yscale('log')
gv.set_axes_limits_and_ticks(axRHS,
ylim=ax.get_ylim(),
yticks=pressures_nice,
yticklabels=heights_nice)
axRHS.tick_params(labelsize=12) # manually set tick label size
axRHS.minorticks_off()

# manually set tick length, width and ticklabel size
axRHS.tick_params(labelsize=18, length=8, width=0.9)

# Use geocat.viz.util convenience function to add titles and the pressure label
axRHS.set_ylabel(ylabel='Height (km)', labelpad=10, fontsize=24)
axRHS = gv.add_height_from_pressure_axis(ax, heights=np.arange(4, 28, 4))

# Force the plot to be square by setting the aspect ratio to 1
ax.set_box_aspect(1)
Expand Down
19 changes: 1 addition & 18 deletions Gallery/Contours/NCL_h_long_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,7 @@
labelfontsize=18)

# Create second y-axis to show geo-potential height.
axRHS = ax.twinx()

# Select heights to display as tick labels
heights_nice = np.arange(0, 32, 4)
# Send "nice" height values back to pressure as tick locations
pressures_nice = mpcalc.height_to_pressure_std(heights_nice *
units('km')).magnitude

axRHS.set_yscale('log')
gv.set_axes_limits_and_ticks(axRHS,
ylim=ax.get_ylim(),
yticks=pressures_nice,
yticklabels=heights_nice)
axRHS.tick_params(labelsize=12) # manually set tick label size
axRHS.minorticks_off()

# Use geocat.viz.util convenience function to add titles and the pressure label
axRHS.set_ylabel(ylabel='Height (km)', labelpad=10, fontsize=18)
axRHS = gv.add_height_from_pressure_axis(ax, heights=np.arange(4, 28, 4))

# Force the plot to be square by setting the aspect ratio to 1
ax.set_box_aspect(1)
Expand Down
56 changes: 0 additions & 56 deletions Gallery/MapProjections/NCL_native_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,53 +27,6 @@
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 @@ -104,15 +57,6 @@ def add_lat_lon_gridlines(ax,
# 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
61 changes: 7 additions & 54 deletions Gallery/MapProjections/NCL_native_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,53 +37,6 @@
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 @@ -115,13 +68,13 @@ def add_lat_lon_gridlines(ax,
ticks=cbar_ticks)

# Draw 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)
gl = gv.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
2 changes: 1 addition & 1 deletion Gallery/Skew-T/NCL_skewt_2_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
This script illustrates the following concepts:
- Customizing the background of a Skew-T plot
- Plotting temperature, dewpoint, and wind data on a Skew-T plot
- Using GeoCAT-comp function `get_skewt_vars <https://geocat-comp.readthedocs.io/en/latest/user_api/generated/geocat.comp.skewt_params.get_skewt_vars.html#geocat.comp.skewt_params.get_skewt_vars>`_ to calculate CAPE, Precipitable Water, Showalter Index, Pressure of the lifting condensation level, and Temperature at the lifting condensation level [C]
- Using GeoCAT-viz function `get_skewt_vars <https://geocat-viz.readthedocs.io/en/latest/user_api/generated/geocat.viz.util.get_skewt_vars.html>`_ to calculate CAPE, Precipitable Water, Showalter Index, Pressure of the lifting condensation level, and Temperature at the lifting condensation level [C]
See following URLs to see the reproduced NCL plot & script:
- Original NCL script: https://www.ncl.ucar.edu/Applications/Scripts/skewt_2.ncl
- Original NCL plots: https://www.ncl.ucar.edu/Applications/Images/skewt_2_2_lg.png
Expand Down
22 changes: 1 addition & 21 deletions Gallery/Vectors/NCL_vector_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,27 +198,7 @@
labelfontsize=28)

# Create second y-axis to show geo-potential height.
axRHS = ax.twinx()

# Select heights to display as tick labels
heights_nice = np.arange(0, 12, 4)
# Send "nice" height values back to pressure as tick locations
pressures_nice = mpcalc.height_to_pressure_std(heights_nice *
units('km')).magnitude

axRHS.set_yscale('log')
gv.set_axes_limits_and_ticks(axRHS,
ylim=ax.get_ylim(),
yticks=pressures_nice,
yticklabels=heights_nice)
axRHS.tick_params(labelsize=12) # manually set tick label size
axRHS.minorticks_off()

# manually set tick length, width and ticklabel size
axRHS.tick_params(labelsize=16, length=8, width=0.9)

# Use geocat.viz.util convenience function to add titles and the pressure label
axRHS.set_ylabel(ylabel='Height (km)', labelpad=10, fontsize=28)
axRHS = gv.add_height_from_pressure_axis(ax, heights=[4, 8, 12])

# Force the plot to be square by setting the aspect ratio to 1
ax.set_box_aspect(1)
Expand Down

0 comments on commit 2aa608b

Please sign in to comment.