Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit agnostic intervals for color-mapped hodographs #3054

Merged
merged 4 commits into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/metpy/plots/skewt.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,10 +890,12 @@ def plot_colormapped(self, u, v, c, intervals=None, colors=None, **kwargs):
variable besides the winds (e.g. heights or pressure levels) and either a colormap to
color it with or a series of contour intervals and colors to create a colormap and
norm to control colormapping. The intervals must always be in increasing
order. For using custom contour intervals with height data, the function will
automatically interpolate to the contour intervals from the height and wind data,
as well as convert the input contour intervals from height AGL to MSL to work with the
provided heights.
order.

When c and intervals are height data (`pint.Quantity` objects with units of length,
such as 'm' or 'km'), the function will automatically interpolate to the contour
intervals from the height and wind data, as well as convert the input contour intervals
from height AGL to MSL to work with the provided heights.

Parameters
----------
Expand Down Expand Up @@ -926,7 +928,7 @@ def plot_colormapped(self, u, v, c, intervals=None, colors=None, **kwargs):
if colors:
cmap = mcolors.ListedColormap(colors)
# If we are segmenting by height (a length), interpolate the contour intervals
if intervals.check('[length]'):
if isinstance(intervals, units.Quantity) and intervals.check('[length]'):
23ccozad marked this conversation as resolved.
Show resolved Hide resolved

# Find any intervals not in the data and interpolate them
heights_min = np.nanmin(c)
Expand All @@ -952,7 +954,8 @@ def plot_colormapped(self, u, v, c, intervals=None, colors=None, **kwargs):
c = c.to_base_units() # TODO: This shouldn't be required!
intervals = intervals.to_base_units()

norm = mcolors.BoundaryNorm(intervals.magnitude, cmap.N)
intervals_m = intervals.m if isinstance(intervals, units.Quantity) else intervals
23ccozad marked this conversation as resolved.
Show resolved Hide resolved
norm = mcolors.BoundaryNorm(intervals_m, cmap.N)
cmap.set_over('none')
cmap.set_under('none')
kwargs['cmap'] = cmap
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions tests/plots/test_skewt.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,54 @@ def test_hodograph_plot_layers_bound_units():
return fig


@pytest.mark.mpl_image_compare(tolerance=0)
def test_hodograph_plot_colors_with_unitless_intervals():
"""Test hodograph colored layers when intervals have no units."""
fig = plt.figure(figsize=(7, 7))
ax = fig.add_subplot(1, 1, 1)
hodo = Hodograph(ax, component_range=50)
hodo.add_grid(10)
u = np.array([0, 6, 26, 32, 48])
v = np.array([0, 23, 34, 23, 5])
p = np.flip(np.array([900, 750, 600, 450, 250]))
intervals = np.flip(np.array([1000, 850, 700, 500, 300, 200]))
colors = ['red', 'green', 'yellow', 'blue', 'purple']
hodo.plot_colormapped(u, v, p, intervals=intervals, colors=colors)
return fig


@pytest.mark.mpl_image_compare(tolerance=0)
def test_hodograph_plot_colors_with_pressure_intervals():
"""Test hodograph colored layers when intervals are given in units of pressure."""
fig = plt.figure(figsize=(7, 7))
ax = fig.add_subplot(1, 1, 1)
hodo = Hodograph(ax, component_range=50)
hodo.add_grid(10)
u = np.array([0, 6, 26, 32, 48])
v = np.array([0, 23, 34, 23, 5])
p = units.Quantity(np.flip(np.array([900, 750, 600, 450, 250])), 'hPa')
intervals = units.Quantity(np.flip(np.array([1000, 850, 700, 500, 300, 200])), 'hPa')
colors = ['red', 'green', 'yellow', 'blue', 'purple']
hodo.plot_colormapped(u, v, p, intervals=intervals, colors=colors)
return fig


@pytest.mark.mpl_image_compare(tolerance=0)
def test_hodograph_plot_colors_with_height_intervals():
"""Test hodograph colored layers when intervals are given in units of height."""
fig = plt.figure(figsize=(7, 7))
ax = fig.add_subplot(1, 1, 1)
hodo = Hodograph(ax, component_range=50)
hodo.add_grid(10)
u = np.array([0, 6, 26, 32, 48])
v = np.array([0, 23, 34, 23, 5])
h = units.Quantity(np.array([0.1, 3.5, 5.5, 10.9, 14.0]), 'km')
intervals = units.Quantity(np.array([0, 3, 6, 9, 12, 15]), 'km')
colors = ['red', 'green', 'yellow', 'blue', 'purple']
hodo.plot_colormapped(u, v, h, intervals=intervals, colors=colors)
return fig


@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
def test_hodograph_plot_arbitrary_layer():
"""Test hodograph colored layers for arbitrary variables without interpolation."""
Expand Down