-
Notifications
You must be signed in to change notification settings - Fork 145
Closed
Description
Aim: add new plot type diurnal_cycle to monitor/multi_datasets.py, i.e., a plot variable (y-axis) vs. hour of day (x-axis).
Large parts of the necessary code can be copy-pasted from here:
ESMValTool/esmvaltool/diag_scripts/monitor/multi_datasets.py
Lines 1270 to 1340 in 0d2b68d
| def create_annual_cycle_plot(self, datasets, short_name): | |
| """Create annual cycle plot.""" | |
| plot_type = 'annual_cycle' | |
| if plot_type not in self.plots: | |
| return | |
| if not datasets: | |
| raise ValueError(f"No input data to plot '{plot_type}' given") | |
| logger.info("Plotting %s", plot_type) | |
| fig = plt.figure(**self.cfg['figure_kwargs']) | |
| axes = fig.add_subplot() | |
| # Plot all datasets in one single figure | |
| ancestors = [] | |
| cubes = {} | |
| for dataset in datasets: | |
| ancestors.append(dataset['filename']) | |
| cube = dataset['cube'] | |
| cubes[self._get_label(dataset)] = cube | |
| self._check_cube_dimensions(cube, plot_type) | |
| # Plot annual cycle | |
| plot_kwargs = self._get_plot_kwargs(plot_type, dataset) | |
| plot_kwargs['axes'] = axes | |
| iris.plot.plot(cube, **plot_kwargs) | |
| # Default plot appearance | |
| multi_dataset_facets = self._get_multi_dataset_facets(datasets) | |
| axes.set_title(multi_dataset_facets['long_name']) | |
| axes.set_xlabel('Month') | |
| axes.set_ylabel(f"{short_name} [{multi_dataset_facets['units']}]") | |
| axes.set_xticks(range(1, 13), [str(m) for m in range(1, 13)]) | |
| gridline_kwargs = self._get_gridline_kwargs(plot_type) | |
| if gridline_kwargs is not False: | |
| axes.grid(**gridline_kwargs) | |
| # Legend | |
| legend_kwargs = self.plots[plot_type]['legend_kwargs'] | |
| if legend_kwargs is not False: | |
| axes.legend(**legend_kwargs) | |
| # Customize plot appearance | |
| self._process_pyplot_kwargs(plot_type, multi_dataset_facets) | |
| # Save plot | |
| plot_path = self.get_plot_path(plot_type, multi_dataset_facets) | |
| fig.savefig(plot_path, **self.cfg['savefig_kwargs']) | |
| logger.info("Wrote %s", plot_path) | |
| plt.close() | |
| # Save netCDF file | |
| netcdf_path = get_diagnostic_filename(Path(plot_path).stem, self.cfg) | |
| var_attrs = { | |
| n: datasets[0][n] for n in ('short_name', 'long_name', 'units') | |
| } | |
| io.save_1d_data(cubes, netcdf_path, 'month_number', var_attrs) | |
| # Provenance tracking | |
| caption = (f"Annual cycle of {multi_dataset_facets['long_name']} for " | |
| f"various datasets.") | |
| provenance_record = { | |
| 'ancestors': ancestors, | |
| 'authors': ['schlund_manuel'], | |
| 'caption': caption, | |
| 'plot_types': ['seas'], | |
| 'long_names': [var_attrs['long_name']], | |
| } | |
| with ProvenanceLogger(self.cfg) as provenance_logger: | |
| provenance_logger.log(plot_path, provenance_record) | |
| provenance_logger.log(netcdf_path, provenance_record) |
Metadata
Metadata
Assignees
Labels
No labels