Skip to content

Commit e4f9ec2

Browse files
authored
Allow setting matplotlib.rcParams in monitor/multi_datasets.py (#3844)
1 parent bd5c33e commit e4f9ec2

File tree

2 files changed

+89
-37
lines changed

2 files changed

+89
-37
lines changed

esmvaltool/diag_scripts/monitor/multi_datasets.py

+81-37
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@
8484
group_variables_by: str, optional (default: 'short_name')
8585
Facet which is used to create variable groups. For each variable group, an
8686
individual plot is created.
87+
matplotlib_rc_params: dict, optional (default: {})
88+
Optional :class:`matplotlib.RcParams` used to customize matplotlib plots.
89+
Options given here will be passed to :func:`matplotlib.rc_context` and used
90+
for all plots produced with this diagnostic. Note: fontsizes specified here
91+
might be overwritten by the plot-type-specific option ``fontsize`` (see
92+
below).
8793
plots: dict, optional
8894
Plot types plotted by this diagnostic (see list above). Dictionary keys
8995
must be ``timeseries``, ``annual_cycle``, ``map``, ``zonal_mean_profile``,
@@ -206,9 +212,11 @@
206212
(top right panel). Thus, the use of the ``plot_kwargs`` ``vmin`` and
207213
``vmax`` or ``levels`` is highly recommend when using this ``common_cbar:
208214
true``. This option has no effect if no reference dataset is given.
209-
fontsize: int, optional (default: 10)
215+
fontsize: int, optional (default: None)
210216
Fontsize used for ticks, labels and titles. For the latter, use the given
211-
fontsize plus 2. Does not affect suptitles.
217+
fontsize plus 2. Does not affect suptitles. If not given, use default
218+
matplotlib values. For a more fine-grained definition of fontsizes, use the
219+
option ``matplotlib_rc_params`` (see above).
212220
gridline_kwargs: dict, optional
213221
Optional keyword arguments for grid lines. By default, ``color: lightgrey,
214222
alpha: 0.5`` are used. Use ``gridline_kwargs: false`` to not show grid
@@ -296,9 +304,11 @@
296304
(top right panel). Thus, the use of the ``plot_kwargs`` ``vmin`` and
297305
``vmax`` or ``levels`` is highly recommend when using this ``common_cbar:
298306
true``. This option has no effect if no reference dataset is given.
299-
fontsize: int, optional (default: 10)
307+
fontsize: int, optional (default: None)
300308
Fontsize used for ticks, labels and titles. For the latter, use the given
301-
fontsize plus 2. Does not affect suptitles.
309+
fontsize plus 2. Does not affect suptitles. If not given, use default
310+
matplotlib values. For a more fine-grained definition of fontsizes, use the
311+
option ``matplotlib_rc_params`` (see above).
302312
log_y: bool, optional (default: True)
303313
Use logarithmic Y-axis.
304314
plot_func: str, optional (default: 'contourf')
@@ -448,9 +458,11 @@
448458
(top right panel). Thus, the use of the ``plot_kwargs`` ``vmin`` and
449459
``vmax`` or ``levels`` is highly recommend when using this ``common_cbar:
450460
true``. This option has no effect if no reference dataset is given.
451-
fontsize: int, optional (default: 10)
461+
fontsize: int, optional (default: None)
452462
Fontsize used for ticks, labels and titles. For the latter, use the given
453-
fontsize plus 2. Does not affect suptitles.
463+
fontsize plus 2. Does not affect suptitles. If not given, use default
464+
matplotlib values. For a more fine-grained definition of fontsizes, use the
465+
option ``matplotlib_rc_params`` (see above).
454466
log_y: bool, optional (default: True)
455467
Use logarithmic Y-axis.
456468
plot_func: str, optional (default: 'contourf')
@@ -535,9 +547,11 @@
535547
(top right panel). Thus, the use of the ``plot_kwargs`` ``vmin`` and
536548
``vmax`` or ``levels`` is highly recommend when using this ``common_cbar:
537549
true``. This option has no effect if no reference dataset is given.
538-
fontsize: int, optional (default: 10)
550+
fontsize: int, optional (default: None)
539551
Fontsize used for ticks, labels and titles. For the latter, use the given
540-
fontsize plus 2. Does not affect suptitles.
552+
fontsize plus 2. Does not affect suptitles. If not given, use default
553+
matplotlib values. For a more fine-grained definition of fontsizes, use the
554+
option ``matplotlib_rc_params`` (see above).
541555
plot_func: str, optional (default: 'contourf')
542556
Plot function used to plot the profiles. Must be a function of
543557
:mod:`iris.plot` that supports plotting of 2D cubes with coordinates
@@ -654,6 +668,7 @@ def __init__(self, config):
654668
self.cfg.setdefault('facet_used_for_labels', 'dataset')
655669
self.cfg.setdefault('figure_kwargs', {'constrained_layout': True})
656670
self.cfg.setdefault('group_variables_by', 'short_name')
671+
self.cfg.setdefault('matplotlib_rc_params', {})
657672
self.cfg.setdefault('savefig_kwargs', {
658673
'bbox_inches': 'tight',
659674
'dpi': 300,
@@ -729,7 +744,7 @@ def __init__(self, config):
729744
)
730745
self.plots[plot_type].setdefault('cbar_kwargs_bias', {})
731746
self.plots[plot_type].setdefault('common_cbar', False)
732-
self.plots[plot_type].setdefault('fontsize', 10)
747+
self.plots[plot_type].setdefault('fontsize', None)
733748
self.plots[plot_type].setdefault('gridline_kwargs', {})
734749
self.plots[plot_type].setdefault('plot_func', 'contourf')
735750
self.plots[plot_type].setdefault('plot_kwargs', {})
@@ -763,7 +778,7 @@ def __init__(self, config):
763778
)
764779
self.plots[plot_type].setdefault('cbar_kwargs_bias', {})
765780
self.plots[plot_type].setdefault('common_cbar', False)
766-
self.plots[plot_type].setdefault('fontsize', 10)
781+
self.plots[plot_type].setdefault('fontsize', None)
767782
self.plots[plot_type].setdefault('log_y', True)
768783
self.plots[plot_type].setdefault('plot_func', 'contourf')
769784
self.plots[plot_type].setdefault('plot_kwargs', {})
@@ -809,7 +824,7 @@ def __init__(self, config):
809824
{'orientation': 'vertical'})
810825
self.plots[plot_type].setdefault('cbar_kwargs_bias', {})
811826
self.plots[plot_type].setdefault('common_cbar', False)
812-
self.plots[plot_type].setdefault('fontsize', 10)
827+
self.plots[plot_type].setdefault('fontsize', None)
813828
self.plots[plot_type].setdefault('log_y', True)
814829
self.plots[plot_type].setdefault('plot_func', 'contourf')
815830
self.plots[plot_type].setdefault('plot_kwargs', {})
@@ -838,7 +853,7 @@ def __init__(self, config):
838853
)
839854
self.plots[plot_type].setdefault('cbar_kwargs_bias', {})
840855
self.plots[plot_type].setdefault('common_cbar', False)
841-
self.plots[plot_type].setdefault('fontsize', 10)
856+
self.plots[plot_type].setdefault('fontsize', None)
842857
self.plots[plot_type].setdefault('plot_func', 'contourf')
843858
self.plots[plot_type].setdefault('plot_kwargs', {})
844859
self.plots[plot_type].setdefault('plot_kwargs_bias', {})
@@ -873,7 +888,9 @@ def __init__(self, config):
873888
def _add_colorbar(self, plot_type, plot_left, plot_right, axes_left,
874889
axes_right, dataset_left, dataset_right):
875890
"""Add colorbar(s) for plots."""
876-
fontsize = self.plots[plot_type]['fontsize']
891+
fontsize = (
892+
self.plots[plot_type]['fontsize'] or mpl.rcParams['axes.labelsize']
893+
)
877894
cbar_kwargs = self._get_cbar_kwargs(plot_type)
878895
cbar_label_left = self._get_cbar_label(plot_type, dataset_left)
879896
cbar_label_right = self._get_cbar_label(plot_type, dataset_right)
@@ -1007,13 +1024,15 @@ def _add_stats(self, plot_type, axes, dim_coords, dataset,
10071024

10081025
def _get_custom_mpl_rc_params(self, plot_type):
10091026
"""Get custom matplotlib rcParams."""
1027+
custom_rc_params = {}
10101028
fontsize = self.plots[plot_type]['fontsize']
1011-
custom_rc_params = {
1012-
'axes.titlesize': fontsize + 2.0,
1013-
'axes.labelsize': fontsize,
1014-
'xtick.labelsize': fontsize,
1015-
'ytick.labelsize': fontsize,
1016-
}
1029+
if fontsize is not None:
1030+
custom_rc_params.update({
1031+
'axes.titlesize': fontsize + 2.0,
1032+
'axes.labelsize': fontsize,
1033+
'xtick.labelsize': fontsize,
1034+
'ytick.labelsize': fontsize,
1035+
})
10171036
return custom_rc_params
10181037

10191038
def _get_label(self, dataset):
@@ -1171,7 +1190,10 @@ def _plot_map_with_ref(self, plot_func, dataset, ref_dataset):
11711190
projection = self._get_map_projection()
11721191
plot_kwargs = self._get_plot_kwargs(plot_type, dataset)
11731192
gridline_kwargs = self._get_gridline_kwargs(plot_type)
1174-
fontsize = self.plots[plot_type]['fontsize']
1193+
fontsize = (
1194+
self.plots[plot_type]['fontsize'] or
1195+
mpl.rcParams['axes.labelsize']
1196+
)
11751197

11761198
# Plot dataset (top left)
11771199
axes_data = fig.add_subplot(gridspec[0:2, 0:2],
@@ -1319,7 +1341,10 @@ def _plot_map_without_ref(self, plot_func, dataset):
13191341
self._add_stats(plot_type, axes, dim_coords_dat, dataset)
13201342

13211343
# Setup colorbar
1322-
fontsize = self.plots[plot_type]['fontsize']
1344+
fontsize = (
1345+
self.plots[plot_type]['fontsize'] or
1346+
mpl.rcParams['axes.labelsize']
1347+
)
13231348
colorbar = fig.colorbar(plot_map, ax=axes,
13241349
**self._get_cbar_kwargs(plot_type))
13251350
colorbar.set_label(self._get_cbar_label(plot_type, dataset),
@@ -1363,7 +1388,10 @@ def _plot_zonal_mean_profile_with_ref(self, plot_func, dataset,
13631388

13641389
# Options used for all subplots
13651390
plot_kwargs = self._get_plot_kwargs(plot_type, dataset)
1366-
fontsize = self.plots[plot_type]['fontsize']
1391+
fontsize = (
1392+
self.plots[plot_type]['fontsize'] or
1393+
mpl.rcParams['axes.labelsize']
1394+
)
13671395

13681396
# Plot dataset (top left)
13691397
axes_data = fig.add_subplot(gridspec[0:2, 0:2])
@@ -1472,7 +1500,10 @@ def _plot_zonal_mean_profile_without_ref(self, plot_func, dataset):
14721500
self._add_stats(plot_type, axes, dim_coords_dat, dataset)
14731501

14741502
# Setup colorbar
1475-
fontsize = self.plots[plot_type]['fontsize']
1503+
fontsize = (
1504+
self.plots[plot_type]['fontsize'] or
1505+
mpl.rcParams['axes.labelsize']
1506+
)
14761507
colorbar = fig.colorbar(plot_zonal_mean_profile, ax=axes,
14771508
**self._get_cbar_kwargs(plot_type))
14781509
colorbar.set_label(self._get_cbar_label(plot_type, dataset),
@@ -1529,7 +1560,10 @@ def _plot_hovmoeller_z_vs_time_without_ref(self, plot_func, dataset):
15291560
self._add_stats(plot_type, axes, dim_coords_dat, dataset)
15301561

15311562
# Setup colorbar
1532-
fontsize = self.plots[plot_type]['fontsize']
1563+
fontsize = (
1564+
self.plots[plot_type]['fontsize'] or
1565+
mpl.rcParams['axes.labelsize']
1566+
)
15331567
colorbar = fig.colorbar(plot_hovmoeller,
15341568
ax=axes,
15351569
**self._get_cbar_kwargs(plot_type))
@@ -1592,7 +1626,10 @@ def _plot_hovmoeller_z_vs_time_with_ref(self, plot_func, dataset,
15921626

15931627
# Options used for all subplots
15941628
plot_kwargs = self._get_plot_kwargs(plot_type, dataset)
1595-
fontsize = self.plots[plot_type]['fontsize']
1629+
fontsize = (
1630+
self.plots[plot_type]['fontsize'] or
1631+
mpl.rcParams['axes.labelsize']
1632+
)
15961633

15971634
# Plot dataset (top left)
15981635
axes_data = fig.add_subplot(gridspec[0:2, 0:2])
@@ -1710,7 +1747,10 @@ def _plot_hovmoeller_time_vs_lat_or_lon_with_ref(self, plot_func, dataset,
17101747

17111748
# Options used for all subplots
17121749
plot_kwargs = self._get_plot_kwargs(plot_type, dataset)
1713-
fontsize = self.plots[plot_type]['fontsize']
1750+
fontsize = (
1751+
self.plots[plot_type]['fontsize'] or
1752+
mpl.rcParams['axes.labelsize']
1753+
)
17141754

17151755
# Plot dataset (top left)
17161756
axes_data = fig.add_subplot(gridspec[0:2, 0:2])
@@ -1837,7 +1877,10 @@ def _plot_hovmoeller_time_vs_lat_or_lon_without_ref(self, plot_func,
18371877
plot_hovmoeller = plot_func(cube, **plot_kwargs)
18381878

18391879
# Setup colorbar
1840-
fontsize = self.plots[plot_type]['fontsize']
1880+
fontsize = (
1881+
self.plots[plot_type]['fontsize'] or
1882+
mpl.rcParams['axes.labelsize']
1883+
)
18411884
colorbar = fig.colorbar(plot_hovmoeller, ax=axes,
18421885
**self._get_cbar_kwargs(plot_type))
18431886
colorbar.set_label(self._get_cbar_label(plot_type, dataset),
@@ -2592,16 +2635,17 @@ def create_hovmoeller_time_vs_lat_or_lon_plot(self, datasets):
25922635

25932636
def compute(self):
25942637
"""Plot preprocessed data."""
2595-
for (var_key, datasets) in self.grouped_input_data.items():
2596-
logger.info("Processing variable %s", var_key)
2597-
self.create_timeseries_plot(datasets)
2598-
self.create_annual_cycle_plot(datasets)
2599-
self.create_map_plot(datasets)
2600-
self.create_zonal_mean_profile_plot(datasets)
2601-
self.create_1d_profile_plot(datasets)
2602-
self.create_variable_vs_lat_plot(datasets)
2603-
self.create_hovmoeller_z_vs_time_plot(datasets)
2604-
self.create_hovmoeller_time_vs_lat_or_lon_plot(datasets)
2638+
with mpl.rc_context(self.cfg['matplotlib_rc_params']):
2639+
for (var_key, datasets) in self.grouped_input_data.items():
2640+
logger.info("Processing variable %s", var_key)
2641+
self.create_timeseries_plot(datasets)
2642+
self.create_annual_cycle_plot(datasets)
2643+
self.create_map_plot(datasets)
2644+
self.create_zonal_mean_profile_plot(datasets)
2645+
self.create_1d_profile_plot(datasets)
2646+
self.create_variable_vs_lat_plot(datasets)
2647+
self.create_hovmoeller_z_vs_time_plot(datasets)
2648+
self.create_hovmoeller_time_vs_lat_or_lon_plot(datasets)
26052649

26062650

26072651
def main():

esmvaltool/recipes/monitor/recipe_monitor_with_refs.yml

+8
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ diagnostics:
165165
plots:
166166
map:
167167
common_cbar: true
168+
fontsize: 10
168169
plot_kwargs_bias:
169170
levels: [-10.0, -7.5, -5.0, -2.5, 0.0, 2.5, 5.0, 7.5, 10.0]
170171

@@ -182,6 +183,7 @@ diagnostics:
182183
plots:
183184
zonal_mean_profile:
184185
common_cbar: true
186+
fontsize: 10
185187
plot_kwargs_bias:
186188
levels: [-10.0, -7.5, -5.0, -2.5, 0.0, 2.5, 5.0, 7.5, 10.0]
187189

@@ -230,6 +232,11 @@ diagnostics:
230232
plot:
231233
<<: *plot_multi_dataset_default
232234
script: monitor/multi_datasets.py
235+
matplotlib_rc_params:
236+
axes.labelsize: small
237+
axes.titlesize: small
238+
xtick.labelsize: x-small
239+
ytick.labelsize: x-small
233240
plots:
234241
hovmoeller_z_vs_time:
235242
plot_func: contourf
@@ -255,5 +262,6 @@ diagnostics:
255262
plots:
256263
hovmoeller_time_vs_lat_or_lon:
257264
common_cbar: true
265+
fontsize: 10
258266
show_x_minor_ticks: false
259267
time_format: '%Y'

0 commit comments

Comments
 (0)