Search code examples
metpy

Stationplot barbs fails with AttributeError: 'list' object has no attribute 'shape'


When trying to use stationplot.plot_barb to plot wind barbs in a station model plot, I get this AttributeError. I assumed this was happening because the function did not want u and v as lists, so I converted them to numpy arrays before passing thru the wind_components function. However, even though my u and v are arrays and not lists, I still get the error.

I've tested wind barbs with metpy's example code, and it works. I even made sure the type of my u array is the same of that returned by metpy's example code.

######## Code that is questionable #########
u, v = wind_components(np.array(data['wind'])*units('knots'), np.array(data['dir'])*units.degree)
print(data['wind'], data['dir'])
print(u,v)
print(type(u))
stationplot.plot_barb(u, v)
############################################
[4.0, 0.0] [250.0, 0.0]
[ 3.75877048 -0.        ] knot [ 1.36808057 -0.        ] knot
<class 'pint.quantity.build_quantity_class.<locals>.Quantity'>

Full traceback:

AttributeError                            Traceback (most recent call last)
<ipython-input-8-b809815aacc3> in <module>()
     22 print(u,v)
     23 print(type(u))
---> 24 stationplot.plot_barb(u, v)
     25 ############################################
     26 

~/.conda/envs/mybase/lib/python3.6/site-packages/metpy/plots/station_plot.py in plot_barb(self, u, v, **kwargs)
    297             except AttributeError:
    298                 pass
--> 299             u, v = self.ax.projection.transform_vectors(trans, self.x, self.y, u, v)
    300 
    301             # Since we've re-implemented CartoPy's barbs, we need to skip calling it here

lib/cartopy/_crs.pyx in cartopy._crs.CRS.transform_vectors()

AttributeError: 'list' object has no attribute 'shape'

Solution

  • So this is actually failing on your lat/lon variables, which I don't see in your code block, but I assume are lists (or at least that's what we've seen before when this issue has cropped up). We have a fix for this that will be released in MetPy 0.11, but in the meantime, you just need to convert your lat/lon lists to Numpy arrays by wrapping them with np.array().