From 266e9a51a4b865939547cdc42097169ac3bcd766 Mon Sep 17 00:00:00 2001 From: Giacomo Caria Date: Tue, 22 Jul 2025 12:22:35 -0400 Subject: [PATCH 1/6] fix log scale --- xarray/plot/dataarray_plot.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/xarray/plot/dataarray_plot.py b/xarray/plot/dataarray_plot.py index 537fbd5bafb..ce9f51674a1 100644 --- a/xarray/plot/dataarray_plot.py +++ b/xarray/plot/dataarray_plot.py @@ -1581,11 +1581,14 @@ def newplotfunc( ) if "contour" in plotfunc.__name__: + from matplotlib import ticker + # extend is a keyword argument only for contour and contourf, but # passing it to the colorbar is sufficient for imshow and # pcolormesh kwargs["extend"] = cmap_params["extend"] - kwargs["levels"] = cmap_params["levels"] + if not isinstance(kwargs["locator"], ticker.LogLocator): + kwargs["levels"] = cmap_params["levels"] # if colors == a single color, matplotlib draws dashed negative # contours. we lose this feature if we pass cmap and not colors if isinstance(colors, str): From 8678c535b2cfa8a4e8eda812b5e20ca6420bbf80 Mon Sep 17 00:00:00 2001 From: Giacomo Caria Date: Wed, 23 Jul 2025 17:56:03 -0400 Subject: [PATCH 2/6] check for locator in kwargs --- xarray/plot/dataarray_plot.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xarray/plot/dataarray_plot.py b/xarray/plot/dataarray_plot.py index ce9f51674a1..2f42995ddcb 100644 --- a/xarray/plot/dataarray_plot.py +++ b/xarray/plot/dataarray_plot.py @@ -1587,7 +1587,9 @@ def newplotfunc( # passing it to the colorbar is sufficient for imshow and # pcolormesh kwargs["extend"] = cmap_params["extend"] - if not isinstance(kwargs["locator"], ticker.LogLocator): + if "locator" in kwargs and not isinstance( + kwargs["locator"], ticker.LogLocator + ): kwargs["levels"] = cmap_params["levels"] # if colors == a single color, matplotlib draws dashed negative # contours. we lose this feature if we pass cmap and not colors From c04bf7ad025aa4229c0482fe009506486cfe1d81 Mon Sep 17 00:00:00 2001 From: Giacomo Caria Date: Thu, 24 Jul 2025 10:48:52 -0400 Subject: [PATCH 3/6] change logic --- xarray/plot/dataarray_plot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xarray/plot/dataarray_plot.py b/xarray/plot/dataarray_plot.py index 2f42995ddcb..f6b8ee1398a 100644 --- a/xarray/plot/dataarray_plot.py +++ b/xarray/plot/dataarray_plot.py @@ -1587,8 +1587,8 @@ def newplotfunc( # passing it to the colorbar is sufficient for imshow and # pcolormesh kwargs["extend"] = cmap_params["extend"] - if "locator" in kwargs and not isinstance( - kwargs["locator"], ticker.LogLocator + if not ( + "locator" in kwargs and isinstance(kwargs["locator"], ticker.LogLocator) ): kwargs["levels"] = cmap_params["levels"] # if colors == a single color, matplotlib draws dashed negative From 0734cf0a1e98993a4de8527766938964498e571c Mon Sep 17 00:00:00 2001 From: Giacomo Caria <44147817+gcaria@users.noreply.github.com> Date: Wed, 30 Jul 2025 17:35:29 -0400 Subject: [PATCH 4/6] Update xarray/plot/dataarray_plot.py Co-authored-by: Illviljan <14371165+Illviljan@users.noreply.github.com> --- xarray/plot/dataarray_plot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/plot/dataarray_plot.py b/xarray/plot/dataarray_plot.py index f6b8ee1398a..820ffc2aa2e 100644 --- a/xarray/plot/dataarray_plot.py +++ b/xarray/plot/dataarray_plot.py @@ -1588,7 +1588,7 @@ def newplotfunc( # pcolormesh kwargs["extend"] = cmap_params["extend"] if not ( - "locator" in kwargs and isinstance(kwargs["locator"], ticker.LogLocator) + isinstance(kwargs.get("locator"), ticker.LogLocator) ): kwargs["levels"] = cmap_params["levels"] # if colors == a single color, matplotlib draws dashed negative From f06154f69d99fc93287cae07da090572c959af17 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Jul 2025 21:36:45 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/plot/dataarray_plot.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/xarray/plot/dataarray_plot.py b/xarray/plot/dataarray_plot.py index 820ffc2aa2e..d3459791a89 100644 --- a/xarray/plot/dataarray_plot.py +++ b/xarray/plot/dataarray_plot.py @@ -1587,9 +1587,7 @@ def newplotfunc( # passing it to the colorbar is sufficient for imshow and # pcolormesh kwargs["extend"] = cmap_params["extend"] - if not ( - isinstance(kwargs.get("locator"), ticker.LogLocator) - ): + if not (isinstance(kwargs.get("locator"), ticker.LogLocator)): kwargs["levels"] = cmap_params["levels"] # if colors == a single color, matplotlib draws dashed negative # contours. we lose this feature if we pass cmap and not colors From cce5b9894e759860affc7e95fa98c4e6f362147b Mon Sep 17 00:00:00 2001 From: Giacomo Caria Date: Wed, 30 Jul 2025 19:13:42 -0400 Subject: [PATCH 6/6] add test --- xarray/tests/test_plot.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 790ee416e82..e4d0aa2d2a2 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -1771,6 +1771,16 @@ def test_levels(self) -> None: artist = self.plotmethod(levels=3) assert artist.extend == "neither" + def test_cbar_logscale(self) -> None: + _darray = self.darray.copy() + _darray.values = np.abs(_darray.values) + _darray.plot.contourf( + norm=mpl.colors.LogNorm(vmin=0.1, vmax=10), locator=mpl.ticker.LogLocator() + ) + ax = plt.gca() + cbar = ax.figure.colorbar(ax.collections[0], ax=ax) + np.testing.assert_array_equal(cbar.get_ticks(), np.logspace(-3, 1, num=5)) + @pytest.mark.slow class TestContour(Common2dMixin, PlotTestCase):