<a id="api-esmvaltool-diag-scripts-emergent-constraints-init"></a>

# Auxiliary functions for emergent constraints scripts

Convenience functions for emergent constraints diagnostics.

**Functions:**

| [`cdf`](#esmvaltool.diag_scripts.emergent_constraints.cdf)(data, pdf)                                      | Calculate cumulative distribution function for a 1-dimensional PDF.                                                                                          |
|------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [`check_metadata`](#esmvaltool.diag_scripts.emergent_constraints.check_metadata)(metadata[, allowed_var_types])       | Check metadata.                                                                                                                                              |
| [`combine_groups`](#esmvaltool.diag_scripts.emergent_constraints.combine_groups)(groups)                              | Combine [`list`](https://docs.python.org/3/builtins/stdtypes.html#list) of groups to a single [`str`](https://docs.python.org/3/builtins/stdtypes.html#str). |
| [`constraint_info_array`](#esmvaltool.diag_scripts.emergent_constraints.constraint_info_array)(x_data, y_data, ...[, ...])   | Get array with all relevant parameters of emergent constraint.                                                                                               |
| [`create_simple_scatterplot`](#esmvaltool.diag_scripts.emergent_constraints.create_simple_scatterplot)(x_data, y_data, ...)      | Create simple scatterplot of an emergent relationship (without saving).                                                                                      |
| [`export_csv`](#esmvaltool.diag_scripts.emergent_constraints.export_csv)(data_frame, attributes, basename, cfg)   | Export CSV file.                                                                                                                                             |
| [`get_caption`](#esmvaltool.diag_scripts.emergent_constraints.get_caption)(attributes, feature, label[, group])    | Construct caption from plotting attributes for (feature, label) pair.                                                                                        |
| [`get_colors`](#esmvaltool.diag_scripts.emergent_constraints.get_colors)(cfg[, groups])                           | Get color palette.                                                                                                                                           |
| [`get_constraint`](#esmvaltool.diag_scripts.emergent_constraints.get_constraint)(x_data, y_data, obs_mean, obs_std)   | Get constraint on target variable.                                                                                                                           |
| [`get_constraint_from_df`](#esmvaltool.diag_scripts.emergent_constraints.get_constraint_from_df)(training_data, ...[, ...])   | Get constraint on target variable from [`pandas.DataFrame`](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame). |
| [`get_groups`](#esmvaltool.diag_scripts.emergent_constraints.get_groups)(training_data[, add_combined_group])     | Extract groups from training data.                                                                                                                           |
| [`get_input_data`](#esmvaltool.diag_scripts.emergent_constraints.get_input_data)(cfg)                                 | Extract input data.                                                                                                                                          |
| [`get_input_files`](#esmvaltool.diag_scripts.emergent_constraints.get_input_files)(cfg[, patterns, ignore_patterns])   | Get input files.                                                                                                                                             |
| [`get_provenance_record`](#esmvaltool.diag_scripts.emergent_constraints.get_provenance_record)(attributes, tags, \*\*kwargs) | Get provenance record.                                                                                                                                       |
| [`get_xy_data_without_nans`](#esmvaltool.diag_scripts.emergent_constraints.get_xy_data_without_nans)(data_frame, ...)           | Get (X, Y) data for `(feature, label)` combination without nans.                                                                                             |
| [`pandas_object_to_cube`](#esmvaltool.diag_scripts.emergent_constraints.pandas_object_to_cube)(pandas_object[, ...])         | Convert pandas object to [`iris.cube.Cube`](https://scitools-iris.readthedocs.io/en/latest/generated/api/iris.cube.html#iris.cube.Cube).                     |
| [`plot_individual_scatterplots`](#esmvaltool.diag_scripts.emergent_constraints.plot_individual_scatterplots)(training_data, ...)    | Plot individual scatterplots for the different groups.                                                                                                       |
| [`plot_merged_scatterplots`](#esmvaltool.diag_scripts.emergent_constraints.plot_merged_scatterplots)(training_data, ...)        | Plot merged scatterplots (all groups in one plot).                                                                                                           |
| [`plot_target_distributions`](#esmvaltool.diag_scripts.emergent_constraints.plot_target_distributions)(training_data, ...)       | Plot distributions of target variable for every feature.                                                                                                     |
| [`regression_line`](#esmvaltool.diag_scripts.emergent_constraints.regression_line)(x_data, y_data[, n_points])         | Return x and y coordinates of the regression line (mean and error).                                                                                          |
| [`set_plot_appearance`](#esmvaltool.diag_scripts.emergent_constraints.set_plot_appearance)(axes, attributes, \*\*kwargs)   | Set appearance of a plot.                                                                                                                                    |
| [`standard_prediction_error`](#esmvaltool.diag_scripts.emergent_constraints.standard_prediction_error)(x_data, y_data)           | Return a function to calculate standard prediction error.                                                                                                    |
| [`target_pdf`](#esmvaltool.diag_scripts.emergent_constraints.target_pdf)(x_data, y_data, obs_mean, obs_std)       | Calculate probability density function (PDF) for target variable.                                                                                            |

### esmvaltool.diag_scripts.emergent_constraints.cdf(data, pdf)

Calculate cumulative distribution function for a 1-dimensional PDF.

* **Parameters:**
  * **data** ([*numpy.ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – Data points (1D array).
  * **pdf** ([*numpy.ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – Corresponding probability density function (PDF).
* **Returns:**
  Corresponding cumulative distribution function (CDF).
* **Return type:**
  [numpy.ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)

### esmvaltool.diag_scripts.emergent_constraints.check_metadata(metadata, allowed_var_types=None)

Check metadata.

* **Parameters:**
  * **metadata** ([*dict*](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Metadata to check.
  * **allowed_var_types** ([*list*](https://docs.python.org/3/builtins/stdtypes.html#list) *of* [*str*](https://docs.python.org/3/builtins/stdtypes.html#str) *,* *optional*) – Allowed var_types, defaults to `ALLOWED_VAR_TYPES`.
* **Raises:**
  * [**KeyError**](https://docs.python.org/3/builtins/exceptions.html#KeyError) – Metadata does not contain necessary keys `'var_type'` and `'tag'`.
  * [**ValueError**](https://docs.python.org/3/builtins/exceptions.html#ValueError) – Got invalid value for key `'var_type'`.

### esmvaltool.diag_scripts.emergent_constraints.combine_groups(groups)

Combine [`list`](https://docs.python.org/3/builtins/stdtypes.html#list) of groups to a single [`str`](https://docs.python.org/3/builtins/stdtypes.html#str).

* **Parameters:**
  **groups** ([*list*](https://docs.python.org/3/builtins/stdtypes.html#list) *of* [*str*](https://docs.python.org/3/builtins/stdtypes.html#str)) – List of group names.
* **Returns:**
  Combined [`str`](https://docs.python.org/3/builtins/stdtypes.html#str).
* **Return type:**
  [str](https://docs.python.org/3/builtins/stdtypes.html#str)

### esmvaltool.diag_scripts.emergent_constraints.constraint_info_array(x_data, y_data, obs_mean, obs_std, n_points=1000, necessary_p_value=None)

Get array with all relevant parameters of emergent constraint.

* **Parameters:**
  * **x_data** ([*numpy.ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – X data of the emergent constraint.
  * **y_data** ([*numpy.ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – Y data of the emergent constraint.
  * **obs_mean** ([*float*](https://docs.python.org/3/builtins/functions.html#float)) – Mean of observational data.
  * **obs_std** ([*float*](https://docs.python.org/3/builtins/functions.html#float)) – Standard deviation of observational data.
  * **n_points** ([*int*](https://docs.python.org/3/builtins/functions.html#int) *,* *optional* *(**default: 1000* *)*) – Number of sampled points for PDF of target variable.
  * **necessary_p_value** ([*float*](https://docs.python.org/3/builtins/functions.html#float) *,* *optional*) – If given, replace constrained mean and standard deviation with
    unconstrained values when p-value of emergent relationship is greater
    than the given necessary p-value.
* **Returns:**
  Array of shape (8,) with the elements:
  : 1. Constrained mean of target variable.
    2. Constrained standard deviation of target variable.
    3. Unconstrained mean of target variable.
    4. Unconstrained standard deviation of target variable.
    5. Slope of emergent relationship.
    6. Intercept of emergent relationship.
    7. Correlation coefficient r of emergent relationship.
    8. p-value of emergent relationship.
* **Return type:**
  [numpy.ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)

### esmvaltool.diag_scripts.emergent_constraints.create_simple_scatterplot(x_data, y_data, obs_mean, obs_std)

Create simple scatterplot of an emergent relationship (without saving).

* **Parameters:**
  * **x_data** ([*numpy.ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – X data of the emergent constraint.
  * **y_data** ([*numpy.ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – Y data of the emergent constraint.
  * **obs_mean** ([*float*](https://docs.python.org/3/builtins/functions.html#float)) – Mean of observational data.
  * **obs_std** ([*float*](https://docs.python.org/3/builtins/functions.html#float)) – Standard deviation of observational data.

### esmvaltool.diag_scripts.emergent_constraints.export_csv(data_frame, attributes, basename, cfg, tags=None)

Export CSV file.

* **Parameters:**
  * **data_frame** ([*pandas.DataFrame*](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame)) – Data to export.
  * **attributes** ([*dict*](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Plot attributes for the different features and the label data. Used to
    retrieve provenance information.
  * **basename** ([*str*](https://docs.python.org/3/builtins/stdtypes.html#str)) – Basename for the name of the file.
  * **cfg** ([*dict*](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Recipe configuration.
  * **tags** (*iterable* *of* [*str*](https://docs.python.org/3/builtins/stdtypes.html#str) *,* *optional*) – Tags for which provenance information should be retrieved (using
    `attributes`). If not specified, use (last level of) columns of the
    given `data_frame`.
* **Returns:**
  Path to the new CSV file.
* **Return type:**
  [str](https://docs.python.org/3/builtins/stdtypes.html#str)

### esmvaltool.diag_scripts.emergent_constraints.get_caption(attributes, feature, label, group=None)

Construct caption from plotting attributes for (feature, label) pair.

* **Parameters:**
  * **attributes** ([*dict*](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Plot attributes.
  * **feature** ([*str*](https://docs.python.org/3/builtins/stdtypes.html#str)) – Feature.
  * **label** ([*str*](https://docs.python.org/3/builtins/stdtypes.html#str)) – Label.
  * **group** ([*str*](https://docs.python.org/3/builtins/stdtypes.html#str) *,* *optional*) – Group.
* **Returns:**
  Caption.
* **Return type:**
  [str](https://docs.python.org/3/builtins/stdtypes.html#str)
* **Raises:**
  [**KeyError**](https://docs.python.org/3/builtins/exceptions.html#KeyError) – `attributes` does not include necessary keys.

### esmvaltool.diag_scripts.emergent_constraints.get_colors(cfg, groups=None)

Get color palette.

* **Parameters:**
  * **cfg** ([*dict*](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Recipe configuration.
  * **groups** ([*list*](https://docs.python.org/3/builtins/stdtypes.html#list) *,* *optional*) – Use to check whether color for combining groups has to be added.
* **Returns:**
  List of colors that can be used for [`matplotlib`](https://matplotlib.org/stable/index.html#module-matplotlib).
* **Return type:**
  [list](https://docs.python.org/3/builtins/stdtypes.html#list)

### esmvaltool.diag_scripts.emergent_constraints.get_constraint(x_data, y_data, obs_mean, obs_std, confidence_level=0.66)

Get constraint on target variable.

* **Parameters:**
  * **x_data** ([*numpy.ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – X data of the emergent constraint.
  * **y_data** ([*numpy.ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – Y data of the emergent constraint.
  * **obs_mean** ([*float*](https://docs.python.org/3/builtins/functions.html#float)) – Mean of observational data.
  * **obs_std** ([*float*](https://docs.python.org/3/builtins/functions.html#float)) – Standard deviation of observational data.
  * **confidence_level** ([*float*](https://docs.python.org/3/builtins/functions.html#float) *,* *optional* *(**default: 0.66* *)*) – Confindence level to estimate the range of the target variable.
* **Returns:**
  Lower confidence limit, best estimate and upper confidence limit of
  target variable.
* **Return type:**
  [tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple) of [float](https://docs.python.org/3/builtins/functions.html#float)

### esmvaltool.diag_scripts.emergent_constraints.get_constraint_from_df(training_data, pred_input_data, confidence_level=0.66)

Get constraint on target variable from [`pandas.DataFrame`](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame).

* **Parameters:**
  * **training_data** ([*pandas.DataFrame*](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame)) – Training data (features, label).
  * **pred_input_data** ([*pandas.DataFrame*](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame)) – Prediction input data (mean and error).
  * **confidence_level** ([*float*](https://docs.python.org/3/builtins/functions.html#float) *,* *optional* *(**default: 0.66* *)*) – Confindence level to estimate the range of the target variable.
* **Returns:**
  Lower confidence limit, best estimate and upper confidence limit of
  target variable.
* **Return type:**
  [tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple) of [float](https://docs.python.org/3/builtins/functions.html#float)

### esmvaltool.diag_scripts.emergent_constraints.get_groups(training_data, add_combined_group=False)

Extract groups from training data.

* **Parameters:**
  * **training_data** ([*pandas.DataFrame*](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame)) – Training data (features, label).
  * **add_combined_group** ([*bool*](https://docs.python.org/3/builtins/functions.html#bool) *,* *optional* *(**default: False* *)*) – Add combined group of all other groups at the beginning of the
    returned [`list`](https://docs.python.org/3/builtins/stdtypes.html#list).
* **Returns:**
  Groups.
* **Return type:**
  [list](https://docs.python.org/3/builtins/stdtypes.html#list) of [str](https://docs.python.org/3/builtins/stdtypes.html#str)

### esmvaltool.diag_scripts.emergent_constraints.get_input_data(cfg)

Extract input data.

Return training data, prediction input data and corresponding attributes.

* **Parameters:**
  **cfg** ([*dict*](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Recipe configuration.
* **Returns:**
  A tuple containing the training data ([`pandas.DataFrame`](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame)), the
  prediction input data ([`pandas.DataFrame`](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame)) and the
  corresponding attributes ([`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)).
* **Return type:**
  [tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple)

### esmvaltool.diag_scripts.emergent_constraints.get_input_files(cfg, patterns=None, ignore_patterns=None)

Get input files.

* **Parameters:**
  * **cfg** ([*dict*](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Recipe configuration.
  * **patterns** ([*list*](https://docs.python.org/3/builtins/stdtypes.html#list) *of* [*str*](https://docs.python.org/3/builtins/stdtypes.html#str) *,* *optional*) – Use only ancestor files that match these patterns as input files.
  * **ignore_patterns** ([*list*](https://docs.python.org/3/builtins/stdtypes.html#list) *of* [*str*](https://docs.python.org/3/builtins/stdtypes.html#str) *,* *optional*) – Ignore input files that match these patterns.
* **Returns:**
  Input files.
* **Return type:**
  [list](https://docs.python.org/3/builtins/stdtypes.html#list) of [str](https://docs.python.org/3/builtins/stdtypes.html#str)

### esmvaltool.diag_scripts.emergent_constraints.get_provenance_record(attributes, tags, \*\*kwargs)

Get provenance record.

* **Parameters:**
  * **attributes** ([*dict*](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Plot attributes. All provenance keys need to start with
    `'provenance_'`.
  * **tags** ([*list*](https://docs.python.org/3/builtins/stdtypes.html#list) *of* [*str*](https://docs.python.org/3/builtins/stdtypes.html#str)) – Tags used to retrieve data from the `attributes` [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict), i.e.
    features and/or label.
  * **\*\*kwargs** (*Keyword arguments*) – Additional `key:value` pairs directly passed to the provenance record
    [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict). All values may include the format strings `{feature}`
    and `{label}`.
* **Returns:**
  Provenance record.
* **Return type:**
  [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)

### esmvaltool.diag_scripts.emergent_constraints.get_xy_data_without_nans(data_frame, feature, label)

Get (X, Y) data for `(feature, label)` combination without nans.

* **Parameters:**
  * **data_frame** ([*pandas.DataFrame*](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame)) – Training data.
  * **feature** ([*str*](https://docs.python.org/3/builtins/stdtypes.html#str)) – Name of the feature data.
  * **label** ([*str*](https://docs.python.org/3/builtins/stdtypes.html#str)) – Name of the label data.
* **Returns:**
  Tuple containing a [`pandas.DataFrame`](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame) for the X axis (feature)
  and a [`pandas.DataFrame`](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame) for the Y axis (label) without
  missing values.
* **Return type:**
  [tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple)

### esmvaltool.diag_scripts.emergent_constraints.pandas_object_to_cube(pandas_object, index_droplevel=None, columns_droplevel=None, \*\*kwargs)

Convert pandas object to [`iris.cube.Cube`](https://scitools-iris.readthedocs.io/en/latest/generated/api/iris.cube.html#iris.cube.Cube).

* **Parameters:**
  * **pandas_object** ([*pandas.DataFrame*](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame) *or* [*pandas.Series*](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.Series.html#pandas.Series)) – Data to convert.
  * **index_droplevel** ([*int*](https://docs.python.org/3/builtins/functions.html#int) *or* [*list*](https://docs.python.org/3/builtins/stdtypes.html#list) *of* [*int*](https://docs.python.org/3/builtins/functions.html#int) *,* *optional*) – Drop levels of index if not `None`.
  * **columns_droplevel** ([*int*](https://docs.python.org/3/builtins/functions.html#int) *or* [*list*](https://docs.python.org/3/builtins/stdtypes.html#list) *of* [*int*](https://docs.python.org/3/builtins/functions.html#int) *,* *optional*) – Drop levels of columns if not `None`. Can only be used if
    `pandas_object` is a [`pandas.DataFrame`](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame).
  * **\*\*kwargs** (*Keyword arguments*) – Keyword arguments used for the cube metadata, e.g. `standard_name`,
    `var_name`, etc.
* **Returns:**
  Data cube.
* **Return type:**
  [iris.cube.Cube](https://scitools-iris.readthedocs.io/en/latest/generated/api/iris.cube.html#iris.cube.Cube)
* **Raises:**
  [**TypeError**](https://docs.python.org/3/builtins/exceptions.html#TypeError) – `columns_droplevel` is used when `pandas_object` is not a
      [`pandas.DataFrame`](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame).

### esmvaltool.diag_scripts.emergent_constraints.plot_individual_scatterplots(training_data, pred_input_data, attributes, basename, cfg)

Plot individual scatterplots for the different groups.

Plot scatterplots for all pairs of `(feature, label)` data (Separate plot
for each group).

* **Parameters:**
  * **training_data** ([*pandas.DataFrame*](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame)) – Training data (features, label).
  * **pred_input_data** ([*pandas.DataFrame*](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame)) – Prediction input data (mean and error).
  * **attributes** ([*dict*](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Plot attributes for the different features and the label data.
  * **basename** ([*str*](https://docs.python.org/3/builtins/stdtypes.html#str)) – Basename for the name of the file.
  * **cfg** ([*dict*](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Recipe configuration.

### esmvaltool.diag_scripts.emergent_constraints.plot_merged_scatterplots(training_data, pred_input_data, attributes, basename, cfg)

Plot merged scatterplots (all groups in one plot).

Plot scatterplots for all pairs of `(feature, label)` data (all groups in
one plot).

* **Parameters:**
  * **training_data** ([*pandas.DataFrame*](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame)) – Training data (features, label).
  * **pred_input_data** ([*pandas.DataFrame*](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame)) – Prediction input data (mean and error).
  * **attributes** ([*dict*](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Plot attributes for the different features and the label data.
  * **basename** ([*str*](https://docs.python.org/3/builtins/stdtypes.html#str)) – Basename for the name of the file.
  * **cfg** ([*dict*](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Recipe configuration.

### esmvaltool.diag_scripts.emergent_constraints.plot_target_distributions(training_data, pred_input_data, attributes, basename, cfg)

Plot distributions of target variable for every feature.

* **Parameters:**
  * **training_data** ([*pandas.DataFrame*](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame)) – Training data (features, label).
  * **pred_input_data** ([*pandas.DataFrame*](https://pandas.pydata.org/pandas-docs/dev/reference/api/pandas.DataFrame.html#pandas.DataFrame)) – Prediction input data (mean and error).
  * **attributes** ([*dict*](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Plot attributes for the different features and the label data.
  * **basename** ([*str*](https://docs.python.org/3/builtins/stdtypes.html#str)) – Basename for the name of the file.
  * **cfg** ([*dict*](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Recipe configuration.

### esmvaltool.diag_scripts.emergent_constraints.regression_line(x_data, y_data, n_points=1000)

Return x and y coordinates of the regression line (mean and error).

* **Parameters:**
  * **x_data** ([*numpy.ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – X data used to fit the linear regression.
  * **y_data** ([*numpy.ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – Y data used to fit the linear regression.
  * **n_points** ([*int*](https://docs.python.org/3/builtins/functions.html#int) *,* *optional* *(**default: 1000* *)*) – Number of points for the regression lines.
* **Returns:**
  [`numpy.ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray) s for the keys `'x'`, `'y'`,
  `'y_minus_err'`, `'y_plus_err'`, `'slope'`, `'intercept'`,
  `'pvalue'` and `'rvalue'`.
* **Return type:**
  [dict](https://docs.python.org/3/builtins/stdtypes.html#dict)

### esmvaltool.diag_scripts.emergent_constraints.set_plot_appearance(axes, attributes, \*\*kwargs)

Set appearance of a plot.

* **Parameters:**
  * **axes** ([*matplotlib.axes.Axes*](https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.html#matplotlib.axes.Axes)) – Matplotlib Axes object which contains the plot.
  * **attributes** ([*dict*](https://docs.python.org/3/builtins/stdtypes.html#dict)) – Plot attributes.
  * **\*\*kwargs** (*Keyword arguments*) – Keyword arguments of the form `plot_option=tag` where `plot_option`
    is something like `plot_title`, `plot_xlabel`, `plot_xlim`, etc.
    and `tag` a key for the plot attributes [`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict) that describes
    which attributes should be considered for that `plot_option`.

### esmvaltool.diag_scripts.emergent_constraints.standard_prediction_error(x_data, y_data)

Return a function to calculate standard prediction error.

The standard prediction error of a linear regression is the error when
predicting a data point which was not used to fit the regression line in
the first place.

* **Parameters:**
  * **x_data** ([*numpy.ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – X data used to fit the linear regression.
  * **y_data** ([*numpy.ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – Y data used to fit the linear regression.
* **Returns:**
  Function that takes a [`float`](https://docs.python.org/3/builtins/functions.html#float) as single argument (representing the
  X value of a new data point) and returns the standard prediction error
  for that.
* **Return type:**
  callable

### esmvaltool.diag_scripts.emergent_constraints.target_pdf(x_data, y_data, obs_mean, obs_std, n_points=1000, necessary_p_value=None)

Calculate probability density function (PDF) for target variable.

* **Parameters:**
  * **x_data** ([*numpy.ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – X data of the emergent constraint.
  * **y_data** ([*numpy.ndarray*](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – Y data of the emergent constraint.
  * **obs_mean** ([*float*](https://docs.python.org/3/builtins/functions.html#float)) – Mean of observational data.
  * **obs_std** ([*float*](https://docs.python.org/3/builtins/functions.html#float)) – Standard deviation of observational data.
  * **n_points** ([*int*](https://docs.python.org/3/builtins/functions.html#int) *,* *optional* *(**default: 1000* *)*) – Number of sampled points for PDF of target variable.
  * **necessary_p_value** ([*float*](https://docs.python.org/3/builtins/functions.html#float) *,* *optional*) – If given, return unconstrained PDF (using Gaussian distribution with
    unconstrained mean and standard deviation) when p-value of emergent
    relationship is greater than the given necessary p-value.
* **Returns:**
  x and y values for the PDF.
* **Return type:**
  [tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple) of [numpy.ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)
