1717 import cartopy .crs
1818 from cartopy .feature import GSHHSFeature
1919 from cartopy .mpl import gridliner
20- from matplotlib import animation , patches
20+ from matplotlib import animation
2121 from matplotlib .artist import Artist
2222 from matplotlib .axes import Axes
23- from matplotlib .collections import PatchCollection
23+ from matplotlib .collections import PolyCollection
2424 from matplotlib .figure import Figure
2525 from shapely .geometry import Polygon
2626 CAN_PLOT = True
3030 IMPORT_EXCEPTION = exc
3131
3232
33- __all___ = ['CAN_PLOT' , 'plot_on_figure' , 'polygon_to_patch ' ]
33+ __all___ = ['CAN_PLOT' , 'plot_on_figure' , 'polygons_to_collection ' ]
3434
3535
3636_requires_plot = requires_extra (extra = 'plot' , import_error = IMPORT_EXCEPTION )
@@ -81,7 +81,7 @@ def bounds_to_extent(bounds: Tuple[float, float, float, float]) -> List[float]:
8181
8282 import cartopy.crs as ccrs
8383 import matplotlib.pyplot as plt
84- from emsarray.plot import bounds_to_extent, polygon_to_patch
84+ from emsarray.plot import bounds_to_extent
8585 from shapely.geometry import Polygon
8686
8787 polygon = Polygon([
@@ -91,44 +91,40 @@ def bounds_to_extent(bounds: Tuple[float, float, float, float]) -> List[float]:
9191 figure = plt.figure(figsize=(10, 8), dpi=100)
9292 axes = plt.subplot(projection=ccrs.PlateCarree())
9393 axes.set_extent(bounds_to_extent(polygon.buffer(0.1).bounds))
94- axes.add_patch(polygon_to_patch(polygon))
95- figure.show()
9694 """
9795 minx , miny , maxx , maxy = bounds
9896 return [minx , maxx , miny , maxy ]
9997
10098
10199@_requires_plot
102- def polygon_to_patch (polygon : Polygon , ** kwargs : Any ) -> patches .Polygon :
103- """
104- Convert a :class:`shapely.geometry.Polygon <Polygon>` to a
105- :class:`matplotlib.patches.Polygon`.
106- """
107- return patches .Polygon (np .transpose (polygon .exterior .xy ), ** kwargs )
108-
109-
110- @_requires_plot
111- def polygons_to_patch_collection (
100+ def polygons_to_collection (
112101 polygons : Iterable [Polygon ],
113102 ** kwargs : Any ,
114- ) -> PatchCollection :
103+ ) -> PolyCollection :
115104 """
116105 Convert a list of Shapely :class:`Polygons <Polygon>`
117- to a matplotlib :class:`~matplotlib.collections.PatchCollection `.
106+ to a matplotlib :class:`~matplotlib.collections.PolyCollection `.
118107
119108 Parameters
120109 ----------
121- polygons : iterable of ` Polygon`
122- The polygons for the patch collection
110+ polygons : iterable of Shapely :class:`Polygons < Polygon> `
111+ The polygons for the poly collection
123112 **kwargs : Any
124- Keyword arguments to pass to the PatchCollection constructor.
113+ Keyword arguments to pass to the PolyCollection constructor.
125114
126115 Returns
127116 -------
128- :class:`matplotlib.collections.PatchCollection `
129- The PatchCollection made up of the polygons passed in.
117+ :class:`matplotlib.collections.PolyCollection `
118+ A PolyCollection made up of the polygons passed in.
130119 """
131- return PatchCollection (map (polygon_to_patch , polygons ), ** kwargs )
120+ return PolyCollection (
121+ verts = [
122+ np .asarray (polygon .exterior .coords )
123+ for polygon in polygons
124+ ],
125+ closed = False ,
126+ ** kwargs
127+ )
132128
133129
134130@_requires_plot
@@ -154,7 +150,7 @@ def plot_on_figure(
154150 This is used to build the polygons and vector quivers.
155151 scalar : :class:`xarray.DataArray`, optional
156152 The data to plot as an :class:`xarray.DataArray`.
157- This will be passed to :meth:`.Convention.make_patch_collection `.
153+ This will be passed to :meth:`.Convention.make_poly_collection `.
158154 vector : tuple of :class:`numpy.ndarray`, optional
159155 The *u* and *v* components of a vector field
160156 as a tuple of :class:`xarray.DataArray`.
@@ -175,18 +171,18 @@ def plot_on_figure(
175171
176172 if scalar is None and vector is None :
177173 # Plot the polygon shapes for want of anything else to draw
178- patches = convention .make_patch_collection ()
179- axes .add_collection (patches )
174+ collection = convention .make_poly_collection ()
175+ axes .add_collection (collection )
180176 if title is None :
181177 title = 'Geometry'
182178
183179 if scalar is not None :
184180 # Plot a scalar variable on the polygons using a colour map
185- patches = convention .make_patch_collection (
181+ collection = convention .make_poly_collection (
186182 scalar , cmap = 'jet' , edgecolor = 'face' )
187- axes .add_collection (patches )
183+ axes .add_collection (collection )
188184 units = scalar .attrs .get ('units' )
189- figure .colorbar (patches , ax = axes , location = 'right' , label = units )
185+ figure .colorbar (collection , ax = axes , location = 'right' , label = units )
190186
191187 if vector is not None :
192188 # Plot a vector variable using a quiver
@@ -230,7 +226,7 @@ def animate_on_figure(
230226 The coordinate values to vary across frames in the animation.
231227 scalar : :class:`xarray.DataArray`, optional
232228 The data to plot as an :class:`xarray.DataArray`.
233- This will be passed to :meth:`.Convention.make_patch_collection `.
229+ This will be passed to :meth:`.Convention.make_poly_collection `.
234230 It should have horizontal dimensions appropriate for this convention,
235231 and a dimension matching the ``coordinate`` parameter.
236232 vector : tuple of :class:`numpy.ndarray`, optional
@@ -273,17 +269,17 @@ def animate_on_figure(
273269 axes .set_aspect (aspect = 'equal' , adjustable = 'datalim' )
274270 axes .title .set_animated (True )
275271
276- patches = None
272+ collection = None
277273 if scalar is not None :
278274 # Plot a scalar variable on the polygons using a colour map
279275 scalar_values = convention .make_linear (scalar ).values [:, convention .mask ]
280- patches = convention .make_patch_collection (
276+ collection = convention .make_poly_collection (
281277 cmap = 'jet' , edgecolor = 'face' ,
282278 clim = (np .nanmin (scalar_values ), np .nanmax (scalar_values )))
283- axes .add_collection (patches )
284- patches .set_animated (True )
279+ axes .add_collection (collection )
280+ collection .set_animated (True )
285281 units = scalar .attrs .get ('units' )
286- figure .colorbar (patches , ax = axes , location = 'right' , label = units )
282+ figure .colorbar (collection , ax = axes , location = 'right' , label = units )
287283
288284 quiver = None
289285 if vector is not None :
@@ -333,9 +329,9 @@ def animate(index: int) -> Iterable[Artist]:
333329 changes .extend (gridlines .xline_artists )
334330 changes .extend (gridlines .yline_artists )
335331
336- if patches is not None :
337- patches .set_array (scalar_values [index ])
338- changes .append (patches )
332+ if collection is not None :
333+ collection .set_array (scalar_values [index ])
334+ changes .append (collection )
339335
340336 if quiver is not None :
341337 quiver .set_UVC (vector_u_values [index ], vector_v_values [index ])
0 commit comments