Skip to content

Commit

Permalink
Wind barb plotting bug fix
Browse files Browse the repository at this point in the history
Handle arrays of Pint objects when plotting wind barbs
  • Loading branch information
23ccozad committed Jun 18, 2021
1 parent 70cd777 commit d8b4f57
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/metpy/plots/station_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .wx_symbols import (current_weather, high_clouds, low_clouds, mid_clouds,
pressure_tendency, sky_cover, wx_symbol_font)
from ..package_tools import Exporter
from ..units import units

exporter = Exporter(globals())

Expand Down Expand Up @@ -328,6 +329,12 @@ def plot_arrow(self, u, v, **kwargs):
@staticmethod
def _vector_plotting_units(u, v, plotting_units):
"""Handle conversion to plotting units for barbs and arrows."""
# If u and v are arrays of Pint objects, put units outside array
if hasattr(u[0], 'units'):
u = units.Quantity(np.fromiter((obj.magnitude for obj in u), float), u[0].units)
if hasattr(v[0], 'units'):
v = units.Quantity(np.fromiter((obj.magnitude for obj in v), float), v[0].units)

if plotting_units:
if hasattr(u, 'units') and hasattr(v, 'units'):
u = u.to(plotting_units)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions tests/plots/test_station_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,24 @@ def test_barb_unit_conversion(barbs_units):
return fig


@pytest.mark.mpl_image_compare(tolerance=0.0048, remove_text=True)
def test_barb_handling_arrays_of_pint_objects(barbs_units):
"""Test handling u and v that are arrays of Pint quantities."""
x_pos = np.array([1])
y_pos = np.array([1])
u_wind = np.array([2 * units.knot], dtype='object')
v_wind = np.array([-4 * units.knot], dtype='object')

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
stnplot = StationPlot(ax, x_pos, y_pos)
stnplot.plot_barb(u_wind, v_wind, plot_units='knots')
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)

return fig


@pytest.mark.mpl_image_compare(tolerance=0.0048, remove_text=True)
def test_arrow_unit_conversion(barbs_units):
"""Test that arrow units can be converted at plot time (#737)."""
Expand Down

0 comments on commit d8b4f57

Please sign in to comment.