Skip to content

Commit 801eb7f

Browse files
authored
BUG: fix plotting with void dimension and no name (#122)
* fix uncaught paths * test * try with load
1 parent 6c897b0 commit 801eb7f

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

xvec/plotting.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,18 @@ def _plot(
269269
cmap_params = {}
270270

271271
# Handle simple case - single geometry with no faceting
272-
if not col and isinstance(arr, xr.DataArray) and arr.ndim == 1:
273-
arr.xvec.to_geodataframe(geometry=geometry).plot(arr.values, ax=axs, **kwargs)
272+
if not col and isinstance(arr, xr.DataArray) and n_cols == 1 and n_rows == 1:
273+
if arr.ndim == 2:
274+
arr = arr.squeeze()
275+
arr.xvec.to_geodataframe(geometry=geometry, name="plotting").plot(
276+
arr.values,
277+
ax=axs,
278+
vmin=cmap_params.get("vmin", None),
279+
vmax=cmap_params.get("vmax", None),
280+
cmap=cmap_params.get("cmap", None),
281+
categories=cmap_params.get("categories", None),
282+
**kwargs,
283+
)
274284
axs.set_xlabel(x_label, fontsize="small")
275285
axs.set_ylabel(y_label, fontsize="small")
276286

@@ -286,7 +296,7 @@ def _plot(
286296
return fig, axs
287297

288298
if not col and geometry in arr.xvec._geom_coords_all:
289-
arr[geometry].drop_vars([geometry]).xvec.to_geodataframe().plot(
299+
arr[geometry].drop_vars([geometry]).xvec.to_geodataframe(name="plotting").plot(
290300
ax=axs, **kwargs
291301
)
292302
axs.set_xlabel(x_label, fontsize="small")
Loading
Loading

xvec/tests/test_plotting.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,37 @@ def test_1d(aggregated):
7878
assert ax.get_ylabel() == "Geodetic latitude\n[degree]"
7979

8080

81+
@image_comparison(
82+
baseline_images=["void_dimension"], extensions=["png"], style=[], tol=0.01
83+
)
84+
def test_void_dimension():
85+
ds = xr.tutorial.open_dataset("eraint_uvz").load()
86+
counties = gpd.read_file(geodatasets.get_path("geoda natregimes")).to_crs(4326)
87+
88+
ds.sel(month=1, level=[200]).z.xvec.zonal_stats(
89+
counties.geometry,
90+
x_coords="longitude",
91+
y_coords="latitude",
92+
all_touched=True,
93+
).xvec.plot()
94+
95+
96+
@image_comparison(baseline_images=["unnamed"], extensions=["png"], style=[], tol=0.01)
97+
def test_unnamed():
98+
ds = xr.tutorial.open_dataset("eraint_uvz").load()
99+
counties = gpd.read_file(geodatasets.get_path("geoda natregimes")).to_crs(4326)
100+
101+
arr = ds.sel(month=1, level=[200]).z
102+
arr.name = None
103+
104+
arr.xvec.zonal_stats(
105+
counties.geometry,
106+
x_coords="longitude",
107+
y_coords="latitude",
108+
all_touched=True,
109+
).sel(level=200).xvec.plot()
110+
111+
81112
@image_comparison(baseline_images=["var_geom"], extensions=["png"], style=[], tol=0.01)
82113
def test_var_geom(glaciers):
83114
f, ax = glaciers.geometry.xvec.plot(col="year")

0 commit comments

Comments
 (0)