Skip to content

Commit 282b697

Browse files
authored
Update default parameters to avoid dynamic plot issues (#1076)
* dynamic plot parameter updates * fix tests * update x and y label * rollback deleted parmater * fix projection logic
1 parent 59ac60b commit 282b697

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

test/test_plot.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ def test_face_centered_data(self):
4646
uxds = ux.open_dataset(gridfile_mpas, gridfile_mpas)
4747

4848
for backend in ['matplotlib', 'bokeh']:
49-
assert(isinstance(uxds['bottomDepth'].plot(backend=backend), hv.DynamicMap))
50-
assert(isinstance(uxds['bottomDepth'].plot.polygons(backend=backend), hv.DynamicMap))
49+
assert(isinstance(uxds['bottomDepth'].plot(backend=backend, dynamic=True), hv.DynamicMap))
50+
assert(isinstance(uxds['bottomDepth'].plot.polygons(backend=backend, dynamic=True), hv.DynamicMap))
5151
assert(isinstance(uxds['bottomDepth'].plot.points(backend=backend), hv.Points))
5252

5353
def test_face_centered_remapped_dim(self):
@@ -56,8 +56,8 @@ def test_face_centered_remapped_dim(self):
5656
uxds = ux.open_dataset(gridfile_ne30, datafile_ne30)
5757

5858
for backend in ['matplotlib', 'bokeh']:
59-
assert(isinstance(uxds['psi'].plot(backend=backend), hv.DynamicMap))
60-
assert(isinstance(uxds['psi'].plot.polygons(backend=backend), hv.DynamicMap))
59+
assert(isinstance(uxds['psi'].plot(backend=backend, dynamic=True), hv.DynamicMap))
60+
assert(isinstance(uxds['psi'].plot.polygons(backend=backend, dynamic=True), hv.DynamicMap))
6161
assert(isinstance(uxds['psi'].plot.points(backend=backend), hv.Points))
6262

6363

@@ -71,7 +71,7 @@ def test_node_centered_data(self):
7171

7272
assert(isinstance(uxds['v1'][0][0].plot.points(backend=backend), hv.Points))
7373

74-
assert(isinstance(uxds['v1'][0][0].topological_mean(destination='face').plot.polygons(backend=backend), hv.DynamicMap))
74+
assert(isinstance(uxds['v1'][0][0].topological_mean(destination='face').plot.polygons(backend=backend, dynamic=True), hv.DynamicMap))
7575

7676

7777

@@ -85,8 +85,8 @@ def test_clabel(self):
8585

8686
def test_engine(self):
8787
uxds = ux.open_dataset(gridfile_mpas, gridfile_mpas)
88-
_plot_sp = uxds['bottomDepth'].plot.polygons(rasterize=True, engine='spatialpandas')
89-
_plot_gp = uxds['bottomDepth'].plot.polygons(rasterize=True, engine='geopandas')
88+
_plot_sp = uxds['bottomDepth'].plot.polygons(rasterize=True, dynamic=True, engine='spatialpandas')
89+
_plot_gp = uxds['bottomDepth'].plot.polygons(rasterize=True, dynamic=True, engine='geopandas')
9090

9191
assert isinstance(_plot_sp, hv.DynamicMap)
9292
assert isinstance(_plot_gp, hv.DynamicMap)

uxarray/plot/accessor.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,14 @@ def __getattr__(self, name: str) -> Any:
345345

346346
def polygons(
347347
self,
348-
periodic_elements="exclude",
349-
backend=None,
350-
engine="spatialpandas",
348+
periodic_elements: Optional[str] = "exclude",
349+
backend: Optional[str] = None,
350+
engine: Optional[str] = "spatialpandas",
351+
rasterize: Optional[bool] = True,
352+
dynamic: Optional[bool] = False,
353+
projection: Optional[ccrs.Projection] = None,
354+
xlabel: Optional[str] = "Longitude",
355+
ylabel: Optional[str] = "Latitude",
351356
*args,
352357
**kwargs,
353358
):
@@ -370,15 +375,14 @@ def polygons(
370375
Plotting backend to use. One of ['matplotlib', 'bokeh']. Equivalent to running holoviews.extension(backend)
371376
engine: str, optional
372377
Engine to use for GeoDataFrame construction. One of ['spatialpandas', 'geopandas']
378+
rasterize: bool, optional
379+
Whether to rasterize the plot (default: True)
380+
projection: ccrs.Projection, optional
381+
The map projection to use.
373382
*args : tuple
374383
Additional positional arguments to be passed to `hvplot.polygons`.
375384
**kwargs : dict
376-
Additional keyword arguments passed to `hvplot.polygons`. These can include:
377-
- "rasterize" (bool): Whether to rasterize the plot (default: True),
378-
- "projection" (ccrs.Projection): The map projection to use (default: `ccrs.PlateCarree()`),
379-
- "clabel" (str): Label for the colorbar, defaulting to the name of the data array (`_uxda.name`),
380-
- "crs" (ccrs.Projection): Coordinate reference system for the plot (default: `ccrs.PlateCarree()`).
381-
For additional customization, please refer to https://hvplot.holoviz.org/user_guide/Customization.html
385+
Additional keyword arguments passed to `hvplot.polygons`. For additional customization, please refer to https://hvplot.holoviz.org/user_guide/Customization.html
382386
383387
Returns
384388
-------
@@ -387,18 +391,22 @@ def polygons(
387391
"""
388392
uxarray.plot.utils.backend.assign(backend)
389393

390-
if "rasterize" not in kwargs:
391-
kwargs["rasterize"] = True
392-
if "projection" not in kwargs:
393-
kwargs["projection"] = ccrs.PlateCarree()
394+
if dynamic and (projection is not None or kwargs.get("geo", None) is True):
395+
warnings.warn(
396+
"Projections with dynamic plots may display incorrectly or update improperly. "
397+
"Consider using static plots instead. See: github.com/holoviz/geoviews/issues/762"
398+
)
399+
400+
if projection is not None:
401+
kwargs["projection"] = projection
402+
print("Hello")
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)
407+
394408
if "clabel" not in kwargs and self._uxda.name is not None:
395409
kwargs["clabel"] = self._uxda.name
396-
if "crs" not in kwargs:
397-
if "projection" in kwargs:
398-
central_longitude = kwargs["projection"].proj4_params["lon_0"]
399-
else:
400-
central_longitude = 0.0
401-
kwargs["crs"] = ccrs.PlateCarree(central_longitude=central_longitude)
402410

403411
gdf = self._uxda.to_geodataframe(
404412
periodic_elements=periodic_elements,
@@ -409,7 +417,10 @@ def polygons(
409417

410418
return gdf.hvplot.polygons(
411419
c=self._uxda.name if self._uxda.name is not None else "var",
412-
geo=True,
420+
rasterize=rasterize,
421+
dynamic=dynamic,
422+
xlabel=xlabel,
423+
ylabel=ylabel,
413424
*args,
414425
**kwargs,
415426
)

0 commit comments

Comments
 (0)