|
7 | 7 | import cartopy.crs as ccrs |
8 | 8 | import hvplot.pandas |
9 | 9 | import hvplot.xarray |
| 10 | +import numpy as np |
10 | 11 | import pandas as pd |
11 | 12 |
|
12 | 13 | import uxarray.plot.dataarray_plot as dataarray_plot |
13 | 14 | import uxarray.plot.utils |
| 15 | +from uxarray.plot.utils import check_crs |
14 | 16 |
|
15 | 17 | if TYPE_CHECKING: |
16 | 18 | from uxarray.core.dataarray import UxDataArray |
@@ -169,6 +171,9 @@ def edges( |
169 | 171 | periodic_elements="exclude", |
170 | 172 | backend=None, |
171 | 173 | engine="spatialpandas", |
| 174 | + rasterize=False, |
| 175 | + geo=True, |
| 176 | + clabel="edges", |
172 | 177 | **kwargs, |
173 | 178 | ): |
174 | 179 | """Plots the edges of a Grid. |
@@ -203,36 +208,57 @@ def edges( |
203 | 208 | ------- |
204 | 209 | gdf.hvplot.paths : hvplot.paths |
205 | 210 | A paths plot of the edges of the unstructured grid |
| 211 | + :param rasterize: |
206 | 212 | """ |
207 | 213 | uxarray.plot.utils.backend.assign(backend) |
208 | 214 |
|
209 | | - if "rasterize" not in kwargs: |
210 | | - kwargs["rasterize"] = False |
211 | | - if "projection" not in kwargs: |
212 | | - kwargs["projection"] = ccrs.PlateCarree() |
213 | | - if "clabel" not in kwargs: |
214 | | - kwargs["clabel"] = "edges" |
215 | | - if "crs" not in kwargs: |
216 | | - if "projection" in kwargs: |
217 | | - central_longitude = kwargs["projection"].proj4_params["lon_0"] |
218 | | - else: |
219 | | - central_longitude = 0.0 |
220 | | - kwargs["crs"] = ccrs.PlateCarree(central_longitude=central_longitude) |
| 215 | + kwargs, periodic_elements = check_crs(kwargs, periodic_elements) |
221 | 216 |
|
222 | 217 | gdf = self._uxgrid.to_geodataframe( |
223 | 218 | periodic_elements=periodic_elements, |
224 | | - projection=kwargs.get("projection"), |
| 219 | + projection=kwargs.get("projection", ccrs.PlateCarree()), |
225 | 220 | engine=engine, |
226 | 221 | project=False, |
227 | 222 | ) |
228 | 223 |
|
229 | | - return gdf.hvplot.paths(geo=True, **kwargs) |
| 224 | + return gdf.hvplot.paths(rasterize=rasterize, geo=geo, clabel=clabel, **kwargs) |
230 | 225 |
|
231 | | - def mesh(self, periodic_elements="exclude", backend=None, **kwargs): |
232 | | - return self.edges(periodic_elements, backend, **kwargs) |
| 226 | + def mesh(self, *args, **kwargs): |
| 227 | + return self.edges(*args, **kwargs) |
233 | 228 |
|
234 | 229 | mesh.__doc__ = edges.__doc__ |
235 | 230 |
|
| 231 | + def polygons( |
| 232 | + self, |
| 233 | + periodic_elements="exclude", |
| 234 | + backend=None, |
| 235 | + engine="spatialpandas", |
| 236 | + rasterize=False, |
| 237 | + geo=True, |
| 238 | + clabel="face index", |
| 239 | + cmap="viridis", |
| 240 | + **kwargs, |
| 241 | + ): |
| 242 | + """Plots the faces of the grid as polygons, shaded by their index.""" |
| 243 | + uxarray.plot.utils.backend.assign(backend) |
| 244 | + |
| 245 | + kwargs, periodic_elements = check_crs(kwargs, periodic_elements) |
| 246 | + |
| 247 | + gdf = self._uxgrid.to_geodataframe( |
| 248 | + periodic_elements=periodic_elements, |
| 249 | + projection=kwargs.get("projection", ccrs.PlateCarree()), |
| 250 | + engine=engine, |
| 251 | + project=False, |
| 252 | + ) |
| 253 | + |
| 254 | + # Use the index of each face as its color |
| 255 | + data = np.arange(self._uxgrid.n_face) |
| 256 | + gdf = gdf.assign(data=data) |
| 257 | + |
| 258 | + return gdf.hvplot.polygons( |
| 259 | + c="data", cmap=cmap, rasterize=rasterize, geo=geo, clabel=clabel, **kwargs |
| 260 | + ) |
| 261 | + |
236 | 262 | def face_degree_distribution( |
237 | 263 | self, |
238 | 264 | backend=None, |
@@ -351,7 +377,6 @@ def polygons( |
351 | 377 | engine: Optional[str] = "spatialpandas", |
352 | 378 | rasterize: Optional[bool] = True, |
353 | 379 | dynamic: Optional[bool] = False, |
354 | | - projection: Optional[ccrs.Projection] = None, |
355 | 380 | xlabel: Optional[str] = "Longitude", |
356 | 381 | ylabel: Optional[str] = "Latitude", |
357 | 382 | *args, |
@@ -392,18 +417,15 @@ def polygons( |
392 | 417 | """ |
393 | 418 | uxarray.plot.utils.backend.assign(backend) |
394 | 419 |
|
395 | | - if dynamic and (projection is not None or kwargs.get("geo", None) is True): |
| 420 | + if dynamic and ( |
| 421 | + kwargs.get("projection", None) is not None |
| 422 | + or kwargs.get("geo", None) is True |
| 423 | + ): |
396 | 424 | warnings.warn( |
397 | 425 | "Projections with dynamic plots may display incorrectly or update improperly. " |
398 | 426 | "Consider using static plots instead. See: github.com/holoviz/geoviews/issues/762" |
399 | 427 | ) |
400 | | - |
401 | | - if projection is not None: |
402 | | - kwargs["projection"] = projection |
403 | | - kwargs["geo"] = True |
404 | | - if "crs" not in kwargs: |
405 | | - central_longitude = projection.proj4_params["lon_0"] |
406 | | - kwargs["crs"] = ccrs.PlateCarree(central_longitude=central_longitude) |
| 428 | + kwargs, periodic_elements = check_crs(kwargs, periodic_elements) |
407 | 429 |
|
408 | 430 | if "clabel" not in kwargs and self._uxda.name is not None: |
409 | 431 | kwargs["clabel"] = self._uxda.name |
|
0 commit comments