From 65a222974edf7b920ccd9b86c79e43a56fa2fca4 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 20 Oct 2025 15:14:29 +0000 Subject: [PATCH 1/2] feat: Add subsample argument to plot_da Adds an optional `subsample` integer argument to the `plot_da` function. When provided, this argument is used to subsample the data along its spatial (x and y) dimensions before plotting, which can improve performance with very large datasets. --- mapflow/_classic.py | 9 ++++++++- tests/test_plot.py | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/mapflow/_classic.py b/mapflow/_classic.py index fe31745..fef024a 100644 --- a/mapflow/_classic.py +++ b/mapflow/_classic.py @@ -266,7 +266,9 @@ def __call__( plt.show() -def plot_da(da: xr.DataArray, x_name=None, y_name=None, crs=None, borders=None, diff=False, **kwargs): +def plot_da( + da: xr.DataArray, x_name=None, y_name=None, crs=None, borders=None, diff=False, subsample=None, **kwargs +): """Convenience function for quick plotting of an xarray DataArray using PlotModel. This is a simplified wrapper around the `PlotModel` class that handles: @@ -288,6 +290,8 @@ def plot_da(da: xr.DataArray, x_name=None, y_name=None, crs=None, borders=None, borders (gpd.GeoDataFrame | gpd.GeoSeries | None): Custom borders to use. If None, defaults to world borders from a packaged GeoPackage. diff (bool, optional): Whether to use a divergent colormap. Defaults to False. + subsample (int, optional): If provided, subsamples the data by this factor for plotting. + Useful for large datasets to speed up plotting. Defaults to None. **kwargs: Additional arguments passed to `PlotModel.__call__`, including: - `figsize` (tuple, optional): Figure size (width, height) in inches. - `qmin`/`qmax` (float, optional): Quantile ranges for color scaling (0-100). @@ -316,6 +320,9 @@ def plot_da(da: xr.DataArray, x_name=None, y_name=None, crs=None, borders=None, actual_x_name = guess_coord_name(da.coords, X_NAME_CANDIDATES, x_name, "x") actual_y_name = guess_coord_name(da.coords, Y_NAME_CANDIDATES, y_name, "y") + if subsample is not None: + da = da.isel({actual_x_name: slice(None, None, subsample), actual_y_name: slice(None, None, subsample)}) + if da[actual_x_name].ndim == 1 and da[actual_y_name].ndim == 1: da = da.sortby(actual_x_name).sortby(actual_y_name) crs_ = process_crs(da, crs) diff --git a/tests/test_plot.py b/tests/test_plot.py index b61c546..d011d0d 100644 --- a/tests/test_plot.py +++ b/tests/test_plot.py @@ -45,3 +45,8 @@ def test_plot_da_with_linestring_borders(air_data): gdf = gpd.GeoDataFrame(geometry=[polygon, line], crs="EPSG:4326") plot_da(da=air_data.isel(time=0), borders=gdf, show=False) plt.close() + + +def test_plot_da_subsample(air_data): + plot_da(da=air_data.isel(time=0), subsample=2, show=False) + plt.close() From d8bce088e020fd2228f66ec8ff51bd8820097278 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Mon, 20 Oct 2025 15:17:04 +0000 Subject: [PATCH 2/2] Format code with Ruff --- mapflow/_classic.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mapflow/_classic.py b/mapflow/_classic.py index fef024a..15e21aa 100644 --- a/mapflow/_classic.py +++ b/mapflow/_classic.py @@ -266,9 +266,7 @@ def __call__( plt.show() -def plot_da( - da: xr.DataArray, x_name=None, y_name=None, crs=None, borders=None, diff=False, subsample=None, **kwargs -): +def plot_da(da: xr.DataArray, x_name=None, y_name=None, crs=None, borders=None, diff=False, subsample=None, **kwargs): """Convenience function for quick plotting of an xarray DataArray using PlotModel. This is a simplified wrapper around the `PlotModel` class that handles: