From 0961c45d29a86a949f946baca238757d4152856f Mon Sep 17 00:00:00 2001 From: Manuel Schlund <32543114+schlunma@users.noreply.github.com> Date: Wed, 30 Oct 2024 12:16:47 +0100 Subject: [PATCH] Use `transform_first=True` for contourf plots with Robinson projection to avoid cartopy bug (#3789) --- esmvaltool/cmorizers/data/formatters/datasets/cmap.py | 2 +- esmvaltool/diag_scripts/monitor/multi_datasets.py | 9 +++++++++ esmvaltool/diag_scripts/shared/plot/_plot.py | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/esmvaltool/cmorizers/data/formatters/datasets/cmap.py b/esmvaltool/cmorizers/data/formatters/datasets/cmap.py index 656942b49a..fecd2b128e 100644 --- a/esmvaltool/cmorizers/data/formatters/datasets/cmap.py +++ b/esmvaltool/cmorizers/data/formatters/datasets/cmap.py @@ -25,8 +25,8 @@ from pathlib import Path import iris -from esmvaltool.cmorizers.data import utilities as utils +from esmvaltool.cmorizers.data import utilities as utils logger = logging.getLogger(__name__) diff --git a/esmvaltool/diag_scripts/monitor/multi_datasets.py b/esmvaltool/diag_scripts/monitor/multi_datasets.py index 32f654b3b6..068c4033da 100644 --- a/esmvaltool/diag_scripts/monitor/multi_datasets.py +++ b/esmvaltool/diag_scripts/monitor/multi_datasets.py @@ -1176,6 +1176,9 @@ def _plot_map_with_ref(self, plot_func, dataset, ref_dataset): axes_data = fig.add_subplot(gridspec[0:2, 0:2], projection=projection) plot_kwargs['axes'] = axes_data + if plot_func is iris.plot.contourf: + # see https://github.com/SciTools/cartopy/issues/2457 + plot_kwargs['transform_first'] = True plot_data = plot_func(cube, **plot_kwargs) axes_data.coastlines() if gridline_kwargs is not False: @@ -1212,6 +1215,9 @@ def _plot_map_with_ref(self, plot_func, dataset, ref_dataset): plot_kwargs_bias = self._get_plot_kwargs(plot_type, dataset, bias=True) plot_kwargs_bias['axes'] = axes_bias + if plot_func is iris.plot.contourf: + # see https://github.com/SciTools/cartopy/issues/2457 + plot_kwargs_bias['transform_first'] = True plot_bias = plot_func(bias_cube, **plot_kwargs_bias) axes_bias.coastlines() if gridline_kwargs is not False: @@ -1268,6 +1274,9 @@ def _plot_map_without_ref(self, plot_func, dataset): axes = fig.add_subplot(projection=self._get_map_projection()) plot_kwargs = self._get_plot_kwargs(plot_type, dataset) plot_kwargs['axes'] = axes + if plot_func is iris.plot.contourf: + # see https://github.com/SciTools/cartopy/issues/2457 + plot_kwargs['transform_first'] = True plot_map = plot_func(cube, **plot_kwargs) axes.coastlines() gridline_kwargs = self._get_gridline_kwargs(plot_type) diff --git a/esmvaltool/diag_scripts/shared/plot/_plot.py b/esmvaltool/diag_scripts/shared/plot/_plot.py index d7db4e1b14..66f1e82c08 100644 --- a/esmvaltool/diag_scripts/shared/plot/_plot.py +++ b/esmvaltool/diag_scripts/shared/plot/_plot.py @@ -228,6 +228,7 @@ def global_contourf(cube, if cbar_range is not None: levels = np.linspace(*cbar_range) kwargs['levels'] = levels + kwargs['transform_first'] = True # see SciTools/cartopy/issues/2457 axes = plt.axes(projection=ccrs.Robinson(central_longitude=10)) plt.sca(axes) map_plot = iris.plot.contourf(cube, **kwargs)