tools.plots
- tools.plots.cmip_plot(ax, df, target, title=None, precip=False, smooth_window=10, agg_level='monthly', target_label='Target', show_target_label=False)[source]
Plots climate model and target data using moving window smoothing.
- tools.plots.cmip_plot_combined(data, target, precip=False, agg_level='monthly', smooth_window=10, target_label='Target', show=False, fig_path=None)[source]
Combines multiple subplots of climate data in different scenarios before and after bias adjustment. Uses moving window smoothing in years instead of days.
- tools.plots.cmip_plot_ensemble(cmip, target, precip=False, intv_sum='ME', intv_mean='YE', figsize=(8, 6), show=True, fig_path=None)[source]
Plots the multi-model mean of climate scenarios including the 90% confidence interval.
- Parameters:
cmip (dict) – A dictionary with keys representing the different CMIP6 models and scenarios as pandas DataFrames containing data of temperature and/or precipitation.
target (pandas.DataFrame) – DataFrame containing the historical reanalysis data.
precip (bool, optional) – If True, plot the mean precipitation. If False, plot the mean temperature. Default is False.
intv_sum (str, optional) – Interval for precipitation sums. Default is monthly (‘ME’).
intv_mean (str, optional) – Interval for the mean of temperature data or precipitation sums. Default is annual (‘YE’).
figsize (tuple, optional) – Figure size for the plot. Default is (8, 6).
show (bool, optional) – If True, show the resulting plot. If False, do not show it. Default is True.
fig_path (str or None, optional) – Path to save the plot. If None, the plot is not saved.
- tools.plots.custom_df_matilda(dic, scenario, var, resample_freq=None)[source]
Takes a dictionary of model outputs and returns a combined dataframe of a specific variable for a given scenario.
- Parameters:
dic (dict) – A nested dictionary of model outputs. The outer keys are scenario names and the inner keys are model names. The corresponding values are dictionaries containing two keys: - ‘model_output’ (DataFrame): containing model outputs for a given scenario and model - ‘glacier_rescaling’ (DataFrame): containing glacier properties for a given scenario and model
scenario (str) – The name of the scenario to select from the dictionary.
var (str) – The name of the variable to extract from the model output DataFrame.
resample_freq (str, optional) – The frequency of the resulting time series data. Defaults to None (i.e. no resampling). If provided, should be in pandas resample frequency string format.
- Returns:
A DataFrame containing the combined data of the specified variable for the selected scenario and models. The DataFrame is indexed by the time steps of the original models. The columns are the names of the models in the selected scenario.
- Return type:
pandas.DataFrame
- Raises:
ValueError – If the provided var string is not one of the following: [‘avg_temp_catchment’, ‘avg_temp_glaciers’, ‘evap_off_glaciers’, ‘prec_off_glaciers’, ‘prec_on_glaciers’, ‘rain_off_glaciers’, ‘snow_off_glaciers’, ‘rain_on_glaciers’, ‘snow_on_glaciers’, ‘snowpack_off_glaciers’, ‘soil_moisture’, ‘upper_groundwater’, ‘lower_groundwater’, ‘melt_off_glaciers’, ‘melt_on_glaciers’, ‘ice_melt_on_glaciers’, ‘snow_melt_on_glaciers’, ‘refreezing_ice’, ‘refreezing_snow’, ‘total_refreezing’, ‘SMB’, ‘actual_evaporation’, ‘total_precipitation’, ‘total_melt’, ‘runoff_without_glaciers’, ‘runoff_from_glaciers’, ‘runoff_ratio’, ‘total_runoff’, ‘glacier_area’, ‘glacier_elev’, ‘smb_water_year’, ‘smb_scaled’, ‘smb_scaled_capped’, ‘smb_scaled_capped_cum’, ‘surplus’, ‘glacier_melt_perc’, ‘glacier_mass_mmwe’, ‘glacier_vol_m3’, ‘glacier_vol_perc’]
- tools.plots.df2long(df, intv_sum='ME', intv_mean='YE', precip=False)[source]
Resamples dataframes and converts them into long format to be passed to seaborn.lineplot().
- tools.plots.ensemble_mean(matilda_scenarios, result_name, dict_name='model_output')[source]
Compute the ensemble mean for a specific variable across all models in the scenarios.
- Parameters:
matilda_scenarios (dict) – Original scenario dictionary with nested model outputs.
result_name (str) – Name of the target variable to extract (e.g., ‘total_runoff’).
dict_name (str) – Name of the dictionary in the model output to look for results (default: ‘model_output’).
- Returns:
A dictionary with emission scenarios as keys and the ensemble mean time series (as pandas Series) as values.
- Return type:
dict
- tools.plots.plot_annual_cycles(matilda_scenarios, scenarios=('SSP2', 'SSP5'), save_path=None)[source]
Creates a 2-column by 4-row plot of annual cycles for the given variables, comparing two scenarios side by side.
- Parameters:
runoff (dict) – Ensemble mean dictionary for runoff.
snow_melt (dict) – Ensemble mean dictionary for snowmelt.
ice_melt (dict) – Ensemble mean dictionary for ice melt.
aet (dict) – Ensemble mean dictionary for actual evaporation.
scenarios (tuple) – Tuple of two scenarios to compare (default: (“SSP2”, “SSP5”))
- Returns:
Displays the subplot figure.
- Return type:
None
- tools.plots.plot_ci_indicators(var, dic, plot_type='line', show=False)[source]
Plot the multi-model mean and confidence intervals of a given variable for two different scenarios.
- Parameters:
var (str) – The variable to plot.
dic (dict, optional) – A dictionary containing the scenarios as keys and the DataFrames as values. Default is matilda_scenarios.
plot_type (str, optional) – Whether the plot should be a line or a bar plot. Default is ‘line’.
show (bool, optional) – Whether to show the resulting plot or not. Default is False.
- Returns:
A plotly figure object containing the mean and confidence intervals for the given variable in the two selected scenarios.
- Return type:
go.Figure
- tools.plots.plot_ci_matilda(var, dic, resample_freq='YE', show=False)[source]
Plot the multi-model mean and confidence intervals of a given variable for two different scenarios.
- Parameters:
var (str) – The variable to plot.
dic (dict, optional) – A dictionary containing the scenarios as keys and the DataFrames as values. Default is matilda_scenarios.
resample_freq (str, optional) – The resampling frequency to apply to the data. Default is ‘YE’.
show (bool, optional) – Whether to show the resulting plot or not. Default is False.
- Returns:
A plotly figure object containing the mean and confidence intervals for the given variable in the two selected scenarios.
- Return type:
go.Figure
- tools.plots.plot_sensitivity_bars(*dfs, labels=None, show=False, bar_width=0.3, bar_gap=0.6)[source]
Plots a horizontal bar chart showing the total sensitivity index for each parameter in a MATILDA model.
- Parameters:
*dfs (pandas.DataFrame) – Multiple dataframes containing the sensitivity indices for each parameter.
labels (list of str, optional) – Labels to use for the different steps in the sensitivity analysis. If not provided, the default labels will be ‘No Limits’, ‘Step 1’, ‘Step 2’, etc.
bar_width (float, optional) – Width of each bar in the chart.
bar_gap (float, optional) – Space between bars.
- tools.plots.pp_matrix(original, target, corrected, scenario=None, nrow=7, ncol=5, precip=False, show=False, fig_path=None)[source]
Arranges the prob_plots of all CMIP6 models in a matrix and adds the R² score.
- Parameters:
original (pandas.DataFrame) – The original climate model data.
target (pandas.DataFrame) – The target data.
corrected (pandas.DataFrame) – The climate model data after bias adjustment.
scenario (str, optional) – The climate scenario to be added to the plot title.
nrow (int, optional) – The number of rows in the plot matrix. Default is 7.
ncol (int, optional) – The number of columns in the plot matrix. Default is 5.
precip (bool, optional) – Indicates whether the data is precipitation data. Default is False.
show (bool, optional) – Indicates whether to display the plot. Default is False.
- Return type:
None
- tools.plots.prob_plot(original, target, corrected, ax, title=None, ylabel='Temperature [K]', **kwargs)[source]
Combines probability plots of climate model data before and after bias adjustment and the target data.
- Parameters:
original (pandas.DataFrame) – The original climate model data.
target (pandas.DataFrame) – The target data.
corrected (pandas.DataFrame) – The climate model data after bias adjustment.
ax (matplotlib.axes.Axes) – The axes on which to plot the probability plot.
title (str, optional) – The title of the plot. Default is None.
ylabel (str, optional) – The label for the y-axis. Default is “Temperature [K]”.
**kwargs (dict, optional) – Additional keyword arguments passed to the probscale.probplot() function.
- Returns:
fig – The generated figure.
- Return type:
matplotlib Figure