Skip to content

Commit

Permalink
Support CenteredNorm in diagnostic monitor/multidatasets.py (#3415)
Browse files Browse the repository at this point in the history
Co-authored-by: Klaus Zimmermann <[email protected]>
  • Loading branch information
schlunma and Klaus Zimmermann authored Nov 14, 2023
1 parent ac28b1f commit 7c1921d
Showing 1 changed file with 72 additions and 27 deletions.
99 changes: 72 additions & 27 deletions esmvaltool/diag_scripts/monitor/multi_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,17 @@
``plot_func``. String arguments can include facets in curly brackets which
will be derived from the corresponding dataset, e.g., ``{project}``,
``{short_name}``, ``{exp}``. Examples: ``default: {levels: 2}, CMIP6:
{vmin: 200, vmax: 250}``.
{vmin: 200, vmax: 250}``. In addition to the normalization_ options
supported by the plot function, the option ``norm: centered`` can be
specified. In this case, the keywords ``vcenter`` and ``halfrange`` should
be used instead of ``vmin`` or ``vmax`` (see
:class:`~matplotlib.colors.CenteredNorm`).
plot_kwargs_bias: dict, optional
Optional keyword arguments for the plot function defined by ``plot_func``
for plotting biases. These keyword arguments update (and potentially
overwrite) the ``plot_kwargs`` for the bias plot. This option has no effect
if no reference dataset is given. See option ``plot_kwargs`` for more
details. By default, uses ``cmap: bwr``.
details. By default, uses ``cmap: bwr`` and ``norm: centered``.
projection: str, optional (default: 'Robinson')
Projection used for the map plot. Needs to be a valid projection class of
:mod:`cartopy.crs`. Keyword arguments can be specified using the option
Expand All @@ -248,10 +252,9 @@
``{project}``, ``{short_name}``, ``{exp}``. Examples: ``title: 'Awesome
Plot of {long_name}'``, ``xlabel: '{short_name}'``, ``xlim: [0, 5]``.
rasterize: bool, optional (default: True)
If ``True``, use `rasterization
<https://matplotlib.org/stable/gallery/misc/rasterization_demo.html>`_ for
map plots to produce smaller files. This is only relevant for vector
graphics (e.g., ``output_file_type=pdf,svg,ps``).
If ``True``, use rasterization_ for map plots to produce smaller files.
This is only relevant for vector graphics (e.g., ``output_file_type:
pdf,svg,ps``).
show_stats: bool, optional (default: True)
Show basic statistics on the plots.
x_pos_stats_avg: float, optional (default: 0.0)
Expand Down Expand Up @@ -309,13 +312,17 @@
``plot_func``. String arguments can include facets in curly brackets which
will be derived from the corresponding dataset, e.g., ``{project}``,
``{short_name}``, ``{exp}``. Examples: ``default: {levels: 2}, CMIP6:
{vmin: 200, vmax: 250}``.
{vmin: 200, vmax: 250}``. In addition to the normalization_ options
supported by the plot function, the option ``norm: centered`` can be
specified. In this case, the keywords ``vcenter`` and ``halfrange`` should
be used instead of ``vmin`` or ``vmax`` (see
:class:`~matplotlib.colors.CenteredNorm`).
plot_kwargs_bias: dict, optional
Optional keyword arguments for the plot function defined by ``plot_func``
for plotting biases. These keyword arguments update (and potentially
overwrite) the ``plot_kwargs`` for the bias plot. This option has no effect
if no reference dataset is given. See option ``plot_kwargs`` for more
details. By default, uses ``cmap: bwr``.
details. By default, uses ``cmap: bwr`` and ``norm: centered``.
pyplot_kwargs: dict, optional
Optional calls to functions of :mod:`matplotlib.pyplot`. Dictionary keys
are functions of :mod:`matplotlib.pyplot`. Dictionary values are used as
Expand All @@ -324,10 +331,9 @@
``{project}``, ``{short_name}``, ``{exp}``. Examples: ``title: 'Awesome
Plot of {long_name}'``, ``xlabel: '{short_name}'``, ``xlim: [0, 5]``.
rasterize: bool, optional (default: True)
If ``True``, use `rasterization
<https://matplotlib.org/stable/gallery/misc/rasterization_demo.html>`_ for
profile plots to produce smaller files. This is only relevant for vector
graphics (e.g., ``output_file_type=pdf,svg,ps``).
If ``True``, use rasterization_ for profile plots to produce smaller files.
This is only relevant for vector graphics (e.g., ``output_file_type:
pdf,svg,ps``).
show_stats: bool, optional (default: True)
Show basic statistics on the plots.
show_y_minor_ticklabels: bool, optional (default: False)
Expand Down Expand Up @@ -456,13 +462,17 @@
``plot_func``. String arguments can include facets in curly brackets which
will be derived from the corresponding dataset, e.g., ``{project}``,
``{short_name}``, ``{exp}``. Examples: ``default: {levels: 2}, CMIP6:
{vmin: 200, vmax: 250}``.
{vmin: 200, vmax: 250}``. In addition to the normalization_ options
supported by the plot function, the option ``norm: centered`` can be
specified. In this case, the keywords ``vcenter`` and ``halfrange`` should
be used instead of ``vmin`` or ``vmax`` (see
:class:`~matplotlib.colors.CenteredNorm`).
plot_kwargs_bias: dict, optional
Optional keyword arguments for the plot function defined by ``plot_func``
for plotting biases. These keyword arguments update (and potentially
overwrite) the ``plot_kwargs`` for the bias plot. This option has no effect
if no reference dataset is given. See option ``plot_kwargs`` for more
details. By default, uses ``cmap: bwr``.
details. By default, uses ``cmap: bwr`` and ``norm: centered``.
pyplot_kwargs: dict, optional
Optional calls to functions of :mod:`matplotlib.pyplot`. Dictionary keys
are functions of :mod:`matplotlib.pyplot`. Dictionary values are used as
Expand All @@ -471,10 +481,9 @@
``{project}``, ``{short_name}``, ``{exp}``. Examples: ``title: 'Awesome
Plot of {long_name}'``, ``xlabel: '{short_name}'``, ``xlim: [0, 5]``.
rasterize: bool, optional (default: True)
If ``True``, use `rasterization
<https://matplotlib.org/stable/gallery/misc/rasterization_demo.html>`_ for
profile plots to produce smaller files. This is only relevant for vector
graphics (e.g., ``output_file_type=pdf,svg,ps``).
If ``True``, use rasterization_ for profile plots to produce smaller files.
This is only relevant for vector graphics (e.g., ``output_file_type:
pdf,svg,ps``).
show_stats: bool, optional (default: True)
Show basic statistics on the plots.
show_y_minor_ticklabels: bool, optional (default: False)
Expand Down Expand Up @@ -536,25 +545,28 @@
``plot_func``. String arguments can include facets in curly brackets which
will be derived from the corresponding dataset, e.g., ``{project}``,
``{short_name}``, ``{exp}``. Examples: ``default: {levels: 2}, CMIP6:
{vmin: 200, vmax: 250}``.
{vmin: 200, vmax: 250}``. In addition to the normalization_ options
supported by the plot function, the option ``norm: centered`` can be
specified. In this case, the keywords ``vcenter`` and ``halfrange`` should
be used instead of ``vmin`` or ``vmax`` (see
:class:`~matplotlib.colors.CenteredNorm`).
plot_kwargs_bias: dict, optional
Optional keyword arguments for the plot function defined by ``plot_func``
for plotting biases. These keyword arguments update (and potentially
overwrite) the ``plot_kwargs`` for the bias plot. This option has no effect
if no reference dataset is given. See option ``plot_kwargs`` for more
details. By default, uses ``cmap: bwr``.
details. By default, uses ``cmap: bwr`` and ``norm: centered``.
pyplot_kwargs: dict, optional
Optional calls to functions of :mod:`matplotlib.pyplot`. Dictionary keys
are functions of :mod:`matplotlib.pyplot`. Dictionary values are used as
single argument for these functions. String arguments can include facets in
curly brackets which will be derived from the corresponding dataset, e.g.,
``{project}``, ``{short_name}``, ``{exp}``. Examples: ``title: 'Awesome
Plot of {long_name}'``, ``xlabel: '{short_name}'``, ``xlim: [0, 5]``.
rasterize: bool, optional (default: False)
If ``True``, use `rasterization
<https://matplotlib.org/stable/gallery/misc/rasterization_demo.html>`_ for
profile plots to produce smaller files. This is only relevant for vector
graphics (e.g., ``output_file_type=pdf,svg,ps``).
rasterize: bool, optional (default: True)
If ``True``, use rasterization_ for profile plots to produce smaller files.
This is only relevant for vector graphics (e.g., ``output_file_type:
pdf,svg,ps``).
show_y_minor_ticks: bool, optional (default: True)
Show minor ticks for time on the Y axis.
show_x_minor_ticks: bool, optional (default: True)
Expand All @@ -570,8 +582,14 @@
anchors to share the configuration of common arguments with other monitor
diagnostic script.
.. _rasterization: https://matplotlib.org/stable/gallery/misc/
rasterization_demo.html
.. _normalization: https://matplotlib.org/stable/users/explain/colors/
colormapnorms.html
"""
import logging
import warnings
from copy import deepcopy
from pathlib import Path
from pprint import pformat
Expand All @@ -586,6 +604,7 @@
from iris.analysis.cartography import area_weights
from iris.coord_categorisation import add_year
from iris.coords import AuxCoord
from matplotlib.colors import CenteredNorm
from matplotlib.gridspec import GridSpec
from matplotlib.ticker import (
AutoMinorLocator,
Expand Down Expand Up @@ -703,6 +722,9 @@ def __init__(self, config):
self.plots[plot_type]['plot_kwargs_bias'].setdefault(
'cmap', 'bwr'
)
self.plots[plot_type]['plot_kwargs_bias'].setdefault(
'norm', 'centered'
)
if 'projection' not in self.plots[plot_type]:
self.plots[plot_type].setdefault('projection', 'Robinson')
self.plots[plot_type].setdefault(
Expand Down Expand Up @@ -734,6 +756,9 @@ def __init__(self, config):
self.plots[plot_type]['plot_kwargs_bias'].setdefault(
'cmap', 'bwr'
)
self.plots[plot_type]['plot_kwargs_bias'].setdefault(
'norm', 'centered'
)
self.plots[plot_type].setdefault('pyplot_kwargs', {})
self.plots[plot_type].setdefault('rasterize', True)
self.plots[plot_type].setdefault('show_stats', True)
Expand Down Expand Up @@ -776,6 +801,9 @@ def __init__(self, config):
self.plots[plot_type].setdefault('plot_kwargs_bias', {})
self.plots[plot_type]['plot_kwargs_bias'].setdefault(
'cmap', 'bwr')
self.plots[plot_type]['plot_kwargs_bias'].setdefault(
'norm', 'centered'
)
self.plots[plot_type].setdefault('pyplot_kwargs', {})
self.plots[plot_type].setdefault('rasterize', True)
self.plots[plot_type].setdefault('show_stats', True)
Expand All @@ -802,8 +830,11 @@ def __init__(self, config):
self.plots[plot_type]['plot_kwargs_bias'].setdefault(
'cmap', 'bwr'
)
self.plots[plot_type]['plot_kwargs_bias'].setdefault(
'norm', 'centered'
)
self.plots[plot_type].setdefault('pyplot_kwargs', {})
self.plots[plot_type].setdefault('rasterize', False)
self.plots[plot_type].setdefault('rasterize', True)
self.plots[plot_type].setdefault(
'show_y_minor_ticks', True
)
Expand Down Expand Up @@ -1043,6 +1074,13 @@ def _get_plot_kwargs(self, plot_type, dataset, bias=False):
'variable_vs_lat'):
plot_kwargs.setdefault('label', label)

if plot_kwargs.get('norm') == 'centered':
norm = CenteredNorm(
vcenter=plot_kwargs.pop('vcenter', 0.0),
halfrange=plot_kwargs.pop('halfrange', None),
)
plot_kwargs['norm'] = norm

return deepcopy(plot_kwargs)

def _load_and_preprocess_data(self):
Expand Down Expand Up @@ -2485,7 +2523,14 @@ def compute(self):
def main():
"""Run diagnostic."""
with run_diagnostic() as config:
MultiDatasets(config).compute()
with warnings.catch_warnings():
warnings.filterwarnings(
'ignore',
message="Using DEFAULT_SPHERICAL_EARTH_RADIUS",
category=UserWarning,
module='iris',
)
MultiDatasets(config).compute()


if __name__ == '__main__':
Expand Down

0 comments on commit 7c1921d

Please sign in to comment.