Search code examples
pythonmatplotlibshapelymetpy

Can MetPy PlotGeometry be used to add a Shapely LineString to a contour plot?


The reference guide for metpy.plots.PlotGeometry states that the geometry attribute is a collection of Shapely objects to plot. Unfortunately, I can't get this to work.

Here is a reduction of the code I tried.

import metpy
from metpy.plots.declarative import PlotGeometry
import cartopy.crs as ccrs
from shapely.geometry import LineString

# Set projection and create line
proj = ccrs.PlateCarree()
geodL = proj.get_geod()
gLine = geodL.inv_intermediate(-95.383056, 29.762778, -81.669722, 41.482222, 20, return_back_azimuth=True)

# create the Shapely LineString and start assembling a MetPy plot
lineShape = LineString(list(zip(gLine.lons, gLine.lats)))
linePlt = PlotGeometry()
linePlt.geometry = lineShape # This fails with traitlets.traitlets.TraitError: The 'geometry' trait of a PlotGeometry instance expected an Iterable, not the LineString <LINESTRING (...

I feel like I'm missing something obvious.


Solution

  • The error message states that the geometry should be a collection, so I suppose replacing linePlt.geometry = lineShape by linePlt.geometry = [lineShape] should help?