Skip to content

Commit 0b93500

Browse files
authored
Unit agnostic intervals for color-mapped hodographs (#3054)
* Allow plot_colormapped() to accept intervals that are array-like. No longer limited to instances of pint.Quantity. * Improved documentation for plot_colormapped() about behavior when c and intervals are height data. * Tests for hodographs where intervals are given with no units, km, or hPa. * Improved type checking with is_quantity
1 parent 42768e0 commit 0b93500

5 files changed

Lines changed: 58 additions & 7 deletions

src/metpy/plots/skewt.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from ..calc.tools import _delete_masked_points
2727
from ..interpolate import interpolate_1d
2828
from ..package_tools import Exporter
29-
from ..units import concatenate, units
29+
from ..units import concatenate, is_quantity, units
3030

3131
exporter = Exporter(globals())
3232

@@ -890,10 +890,12 @@ def plot_colormapped(self, u, v, c, intervals=None, colors=None, **kwargs):
890890
variable besides the winds (e.g. heights or pressure levels) and either a colormap to
891891
color it with or a series of contour intervals and colors to create a colormap and
892892
norm to control colormapping. The intervals must always be in increasing
893-
order. For using custom contour intervals with height data, the function will
894-
automatically interpolate to the contour intervals from the height and wind data,
895-
as well as convert the input contour intervals from height AGL to MSL to work with the
896-
provided heights.
893+
order.
894+
895+
When c and intervals are height data (`pint.Quantity` objects with units of length,
896+
such as 'm' or 'km'), the function will automatically interpolate to the contour
897+
intervals from the height and wind data, as well as convert the input contour intervals
898+
from height AGL to MSL to work with the provided heights.
897899
898900
Parameters
899901
----------
@@ -926,7 +928,7 @@ def plot_colormapped(self, u, v, c, intervals=None, colors=None, **kwargs):
926928
if colors:
927929
cmap = mcolors.ListedColormap(colors)
928930
# If we are segmenting by height (a length), interpolate the contour intervals
929-
if intervals.check('[length]'):
931+
if is_quantity(intervals) and intervals.check('[length]'):
930932

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

955-
norm = mcolors.BoundaryNorm(intervals.magnitude, cmap.N)
957+
intervals_m = intervals.m if is_quantity(intervals) else intervals
958+
norm = mcolors.BoundaryNorm(intervals_m, cmap.N)
956959
cmap.set_over('none')
957960
cmap.set_under('none')
958961
kwargs['cmap'] = cmap
55.5 KB
Loading
55.2 KB
Loading
55.2 KB
Loading

tests/plots/test_skewt.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,54 @@ def test_hodograph_plot_layers_bound_units():
490490
return fig
491491

492492

493+
@pytest.mark.mpl_image_compare(tolerance=0)
494+
def test_hodograph_plot_colors_with_unitless_intervals():
495+
"""Test hodograph colored layers when intervals have no units."""
496+
fig = plt.figure(figsize=(7, 7))
497+
ax = fig.add_subplot(1, 1, 1)
498+
hodo = Hodograph(ax, component_range=50)
499+
hodo.add_grid(10)
500+
u = np.array([0, 6, 26, 32, 48])
501+
v = np.array([0, 23, 34, 23, 5])
502+
p = np.flip(np.array([900, 750, 600, 450, 250]))
503+
intervals = np.flip(np.array([1000, 850, 700, 500, 300, 200]))
504+
colors = ['red', 'green', 'yellow', 'blue', 'purple']
505+
hodo.plot_colormapped(u, v, p, intervals=intervals, colors=colors)
506+
return fig
507+
508+
509+
@pytest.mark.mpl_image_compare(tolerance=0)
510+
def test_hodograph_plot_colors_with_pressure_intervals():
511+
"""Test hodograph colored layers when intervals are given in units of pressure."""
512+
fig = plt.figure(figsize=(7, 7))
513+
ax = fig.add_subplot(1, 1, 1)
514+
hodo = Hodograph(ax, component_range=50)
515+
hodo.add_grid(10)
516+
u = np.array([0, 6, 26, 32, 48])
517+
v = np.array([0, 23, 34, 23, 5])
518+
p = units.Quantity(np.flip(np.array([900, 750, 600, 450, 250])), 'hPa')
519+
intervals = units.Quantity(np.flip(np.array([1000, 850, 700, 500, 300, 200])), 'hPa')
520+
colors = ['red', 'green', 'yellow', 'blue', 'purple']
521+
hodo.plot_colormapped(u, v, p, intervals=intervals, colors=colors)
522+
return fig
523+
524+
525+
@pytest.mark.mpl_image_compare(tolerance=0)
526+
def test_hodograph_plot_colors_with_height_intervals():
527+
"""Test hodograph colored layers when intervals are given in units of height."""
528+
fig = plt.figure(figsize=(7, 7))
529+
ax = fig.add_subplot(1, 1, 1)
530+
hodo = Hodograph(ax, component_range=50)
531+
hodo.add_grid(10)
532+
u = np.array([0, 6, 26, 32, 48])
533+
v = np.array([0, 23, 34, 23, 5])
534+
h = units.Quantity(np.array([0.1, 3.5, 5.5, 10.9, 14.0]), 'km')
535+
intervals = units.Quantity(np.array([0, 3, 6, 9, 12, 15]), 'km')
536+
colors = ['red', 'green', 'yellow', 'blue', 'purple']
537+
hodo.plot_colormapped(u, v, h, intervals=intervals, colors=colors)
538+
return fig
539+
540+
493541
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
494542
def test_hodograph_plot_arbitrary_layer():
495543
"""Test hodograph colored layers for arbitrary variables without interpolation."""

0 commit comments

Comments
 (0)