From f993a9f1e4efe710c6561ec7c4dc4e28d3f51bab Mon Sep 17 00:00:00 2001 From: Le Roux Erwan <erwan.le-roux@irstea.fr> Date: Wed, 23 Dec 2020 11:06:48 +0100 Subject: [PATCH] [contrasting] plots modification after nico's comments --- .../altitudes_fit/main_altitudes_studies.py | 8 +++---- ...es_visualizer_for_non_stationary_models.py | 6 ++--- .../one_fold_analysis/one_fold_fit.py | 6 ++--- .../one_fold_analysis/plot_total_aic.py | 16 ++++++------- .../altitudes_fit/plot_mean_and_std.py | 8 +++++++ ...pute_histogram_change_in_total_snowfall.py | 2 +- .../plots/plot_coherence_curves.py | 16 ++++++------- .../plots/plot_histogram_altitude_studies.py | 23 +++++++++++++++---- 8 files changed, 52 insertions(+), 33 deletions(-) create mode 100644 projects/altitude_spatial_model/altitudes_fit/plot_mean_and_std.py diff --git a/projects/altitude_spatial_model/altitudes_fit/main_altitudes_studies.py b/projects/altitude_spatial_model/altitudes_fit/main_altitudes_studies.py index 92bcc6d8..4fdf8295 100644 --- a/projects/altitude_spatial_model/altitudes_fit/main_altitudes_studies.py +++ b/projects/altitude_spatial_model/altitudes_fit/main_altitudes_studies.py @@ -55,12 +55,12 @@ def main_loop(altitudes_list, massif_names, seasons, study_classes): def plot_visualizers(massif_names, visualizer_list): - # plot_histogram_all_trends_against_altitudes(massif_names, visualizer_list) + plot_histogram_all_trends_against_altitudes(massif_names, visualizer_list) for relative in [True, False]: - # plot_shoe_plot_changes_against_altitude(massif_names, visualizer_list, relative=relative) - plot_shoe_plot_changes_against_altitude_for_maxima_and_total(massif_names, visualizer_list, relative=relative) + plot_shoe_plot_changes_against_altitude(massif_names, visualizer_list, relative=relative) + # plot_shoe_plot_changes_against_altitude_for_maxima_and_total(massif_names, visualizer_list, relative=relative) # plot_coherence_curves(massif_names, visualizer_list) - # plot_coherence_curves(['Vanoise'], visualizer_list) + plot_coherence_curves(['Vanoise'], visualizer_list) def plot_visualizer(massif_names, visualizer): diff --git a/projects/altitude_spatial_model/altitudes_fit/one_fold_analysis/altitudes_studies_visualizer_for_non_stationary_models.py b/projects/altitude_spatial_model/altitudes_fit/one_fold_analysis/altitudes_studies_visualizer_for_non_stationary_models.py index 26b3c970..7678bc8b 100644 --- a/projects/altitude_spatial_model/altitudes_fit/one_fold_analysis/altitudes_studies_visualizer_for_non_stationary_models.py +++ b/projects/altitude_spatial_model/altitudes_fit/one_fold_analysis/altitudes_studies_visualizer_for_non_stationary_models.py @@ -186,8 +186,8 @@ class AltitudesStudiesVisualizerForNonStationaryModels(StudyVisualizer): @property def add_colorbar(self): - return isinstance(self.altitude_group, (VeyHighAltitudeGroup)) - # return isinstance(self.altitude_group, (VeyHighAltitudeGroup, MidAltitudeGroup)) + # return isinstance(self.altitude_group, (VeyHighAltitudeGroup)) + return isinstance(self.altitude_group, (VeyHighAltitudeGroup, MidAltitudeGroup)) def plot_against_years(self, method_name, order): ax = plt.gca() @@ -498,5 +498,5 @@ class AltitudesStudiesVisualizerForNonStationaryModels(StudyVisualizer): ax.set_yticks(ticks) ax.tick_params(labelsize=15) plot_name = 'qqplot/{}'.format(massif_name_corrected) - self.studies.show_or_save_to_file(plot_name=plot_name, show=self.show) + self.studies.show_or_save_to_file(plot_name=plot_name, show=self.show, no_title=True) plt.close() diff --git a/projects/altitude_spatial_model/altitudes_fit/one_fold_analysis/one_fold_fit.py b/projects/altitude_spatial_model/altitudes_fit/one_fold_analysis/one_fold_fit.py index b41207fc..5f5880dd 100644 --- a/projects/altitude_spatial_model/altitudes_fit/one_fold_analysis/one_fold_fit.py +++ b/projects/altitude_spatial_model/altitudes_fit/one_fold_analysis/one_fold_fit.py @@ -100,7 +100,7 @@ class OneFoldFit(object): def relative_change_in_return_level_for_reference_altitude(self) -> float: return self.relative_changes_of_moment(altitudes=[self.altitude_plot], order=None)[0] - def changes_of_moment(self, altitudes, year=2019, nb_years=50, order=1): + def changes_of_moment(self, altitudes, year=2019, nb_years=60, order=1): changes = [] for altitude in altitudes: mean_after = self.get_moment(altitude, year, order) @@ -109,7 +109,7 @@ class OneFoldFit(object): changes.append(change) return changes - def relative_changes_of_moment(self, altitudes, year=2019, nb_years=50, order=1): + def relative_changes_of_moment(self, altitudes, year=2019, nb_years=60, order=1): relative_changes = [] for altitude in altitudes: mean_after = self.get_moment(altitude, year, order) @@ -293,7 +293,7 @@ class OneFoldFit(object): def sign_of_change(self, estimator): return_levels = [] - for year in [2019 - 50, 2019]: + for year in [2019 - 60, 2019]: coordinate = np.array([self.altitude_plot, year]) return_level = estimator.function_from_fit.get_params( coordinate=coordinate, diff --git a/projects/altitude_spatial_model/altitudes_fit/one_fold_analysis/plot_total_aic.py b/projects/altitude_spatial_model/altitudes_fit/one_fold_analysis/plot_total_aic.py index fd74ea7d..6ebcbd94 100644 --- a/projects/altitude_spatial_model/altitudes_fit/one_fold_analysis/plot_total_aic.py +++ b/projects/altitude_spatial_model/altitudes_fit/one_fold_analysis/plot_total_aic.py @@ -2,6 +2,9 @@ import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np +from projects.altitude_spatial_model.altitudes_fit.one_fold_analysis.altitudes_studies_visualizer_for_non_stationary_models import \ + AltitudesStudiesVisualizerForNonStationaryModels + mpl.rcParams['text.usetex'] = True mpl.rcParams['text.latex.preamble'] = [r'\usepackage{amsmath}'] @@ -10,17 +13,12 @@ from projects.altitude_spatial_model.altitudes_fit.one_fold_analysis.one_fold_fi from projects.exceeding_snow_loads.utils import dpi_paper1_figure -def plots(visualizer): - visualizer.plot_shape_map() +def plots(visualizer: AltitudesStudiesVisualizerForNonStationaryModels): + # visualizer.plot_shape_map() visualizer.plot_moments() # visualizer.plot_qqplots() - - - # for plot_mean in [True, False]: - # visualizer.plot_year_for_the_peak(plot_mean=plot_mean) - # visualizer.plot_best_coef_maps() - # visualizer.plot_peak_year_against_altitude() - # visualizer.plot_altitude_switch_against_peak_year() + # for std in [True, False]: + # visualizer.studies.plot_mean_maxima_against_altitude(std=std) def plot_individual_aic(visualizer): diff --git a/projects/altitude_spatial_model/altitudes_fit/plot_mean_and_std.py b/projects/altitude_spatial_model/altitudes_fit/plot_mean_and_std.py new file mode 100644 index 00000000..20bc2fc7 --- /dev/null +++ b/projects/altitude_spatial_model/altitudes_fit/plot_mean_and_std.py @@ -0,0 +1,8 @@ +from extreme_data.meteo_france_data.scm_models_data.safran.safran import SafranSnowfall1Day +from projects.altitude_spatial_model.altitudes_fit.altitudes_studies import AltitudesStudies + +if __name__ == '__main__': + studies = AltitudesStudies(study_class=SafranSnowfall1Day, + altitudes=[600, 900, 1200, 1500, 1800, 2100, 2400, 2700, 3000, 3300, 3600]) + for std in [True, False]: + studies.plot_mean_maxima_against_altitude(std=std) \ No newline at end of file diff --git a/projects/altitude_spatial_model/altitudes_fit/plots/compute_histogram_change_in_total_snowfall.py b/projects/altitude_spatial_model/altitudes_fit/plots/compute_histogram_change_in_total_snowfall.py index 65e3e902..aa978614 100644 --- a/projects/altitude_spatial_model/altitudes_fit/plots/compute_histogram_change_in_total_snowfall.py +++ b/projects/altitude_spatial_model/altitudes_fit/plots/compute_histogram_change_in_total_snowfall.py @@ -32,7 +32,7 @@ def compute_changes_in_total_snowfall(visualizer_list: List[ def compute_change_in_total(study, massif_name, relative, plot=False): annual_total = study.massif_name_to_annual_total[massif_name] a, b, r2score = fit_linear_regression(study.ordered_years, annual_total) - years_for_change = [1969, 2019] + years_for_change = [1959, 2019] values = [a * y + b for y in years_for_change] change = values[1] - values[0] if relative: diff --git a/projects/altitude_spatial_model/altitudes_fit/plots/plot_coherence_curves.py b/projects/altitude_spatial_model/altitudes_fit/plots/plot_coherence_curves.py index f0b69b96..06764d38 100644 --- a/projects/altitude_spatial_model/altitudes_fit/plots/plot_coherence_curves.py +++ b/projects/altitude_spatial_model/altitudes_fit/plots/plot_coherence_curves.py @@ -25,11 +25,11 @@ def plot_coherence_curves(massif_names, visualizer_list: List[AltitudesStudiesVi if i % 2 == 1: ax.set_yticks([]) axes = [ax if i % 2 == 0 else ax.twinx() for i, ax in enumerate(axes)] - colors = ['blue', 'yellow', 'green'] - labels = ['Elevational-temporal model in 2019', 'Elevational-temporal model in 1969', 'Pointwise distribution'] + colors = ['tab:orange', 'blue', 'green'] + labels = ['Elevational-temporal model in 1959', 'Elevational-temporal model in 2019', 'Pointwise distribution'] altitudinal_model = [True, True, False] - years = [2019, 1969, None] - for color, global_label, boolean, year in list(zip(colors, labels, altitudinal_model, years))[::2]: + years = [1959, 2019, None] + for color, global_label, boolean, year in list(zip(colors, labels, altitudinal_model, years))[:]: plot_coherence_curve(axes, massif_name, visualizer_list, boolean, color, global_label, year, legend) visualizer.plot_name = '{}/{}'.format(folder, massif_name.replace('_', '-')) visualizer.show_or_save_to_file(add_classic_title=False, no_title=True, dpi=200) @@ -41,7 +41,7 @@ def plot_coherence_curve(axes, massif_name, visualizer_list: List[AltitudesStudi x_all_list, values_all_list, labels, all_bound_list = load_all_list(massif_name, visualizer_list, is_altitudinal, year) - legend_line = False + legend_line = True for i, label in enumerate(labels): if legend and i != 3: continue @@ -71,18 +71,18 @@ def plot_coherence_curve(axes, massif_name, visualizer_list: List[AltitudesStudi ax.plot(x_list, value_list_dotted, linestyle='dotted', color=color) # Plot confidence interval - if i == 3: + if i == 3 and year in [None, 2019]: for j, (x_list, bounds) in enumerate(list(zip(x_all_list, all_bound_list))): if len(bounds) > 0: lower_bound, upper_bound = bounds if legend and not legend_line: - model_name = 'elevational-temporal model' if is_altitudinal else 'pointwise distribution' + model_name = 'elevational-temporal model in 2019' if is_altitudinal else 'pointwise distribution' fill_label = "95\% confidence interval for the {}".format(model_name) if j == 0 else None ax.fill_between(x_list, lower_bound, upper_bound, color=color, alpha=0.2, label=fill_label) else: ax.fill_between(x_list, lower_bound, upper_bound, color=color, alpha=0.2) - if legend and is_altitudinal: + if legend: min, max = ax.get_ylim() ax.set_ylim([min, 2 * max]) size = 15 if legend_line else 11 diff --git a/projects/altitude_spatial_model/altitudes_fit/plots/plot_histogram_altitude_studies.py b/projects/altitude_spatial_model/altitudes_fit/plots/plot_histogram_altitude_studies.py index 1e839ae8..2def3c39 100644 --- a/projects/altitude_spatial_model/altitudes_fit/plots/plot_histogram_altitude_studies.py +++ b/projects/altitude_spatial_model/altitudes_fit/plots/plot_histogram_altitude_studies.py @@ -124,8 +124,8 @@ def plot_shoe_plot_changes_against_altitude(massif_names, visualizer_list: List[ all_changes = [v.all_changes(massif_names, relative=relative) for v in visualizer_list] all_changes = list(zip(*all_changes)) - labels = ['All selected models', 'Temporally non-stationary models', - 'Temporally non-stationary models which are significant'] + labels = ['All massifs', 'Massifs with a selected model temporally non-stationary', + 'Massifs with a selected model temporally non-stationary and significant'] colors = ['darkgreen', 'forestgreen', 'limegreen'] nb_massifs = [len(v.get_valid_names(massif_names)) for v in visualizer_list] @@ -137,6 +137,16 @@ def plot_shoe_plot_changes_against_altitude(massif_names, visualizer_list: List[ labelsize = 10 linewidth = 3 + # x = np.array([4 * width * (i + 1) for i in range(len(nb_massifs))]) + # for j, (changes, label, color) in enumerate(list(zip(all_changes, labels, colors)), -1): + # positions = x + j * width + # bplot = ax.boxplot(list(changes), positions=positions, widths=width, patch_artist=True, showmeans=True) + # for patch in bplot['boxes']: + # patch.set_facecolor(color) + # x = np.array([3 * width * (i + 1) for i in range(len(nb_massifs))]) + # for j, (changes, label, color) in list(enumerate(list(zip(all_changes, labels, colors)), -1))[1:]: + # positions = x + (j + 0.5) * width + x = np.array([4 * width * (i + 1) for i in range(len(nb_massifs))]) for j, (changes, label, color) in enumerate(list(zip(all_changes, labels, colors)), -1): positions = x + j * width @@ -145,12 +155,12 @@ def plot_shoe_plot_changes_against_altitude(massif_names, visualizer_list: List[ patch.set_facecolor(color) custom_lines = [Line2D([0], [0], color=color, lw=4) for color in colors] - loc = 'lower right' if relative else 'upper left' + loc = 'upper left' ax.legend(custom_lines, labels, loc=loc) start = 'Relative changes' if relative else 'Changes' unit = '\%' if relative else visualizer.study.variable_unit - ax.set_ylabel('{} of {}-year return levels between 1969 and 2019 ({})'.format(start, OneFoldFit.return_period, + ax.set_ylabel('{} of {}-year return levels between 1959 and 2019 ({})'.format(start, OneFoldFit.return_period, unit), fontsize=legend_fontsize) ax.set_xlabel('Elevation', fontsize=legend_fontsize + 5) @@ -163,6 +173,9 @@ def plot_shoe_plot_changes_against_altitude(massif_names, visualizer_list: List[ shift = 2 * width ax.set_xlim((min(x) - shift, max(x) + shift)) + upper_limit_for_legend = 30 if relative else 0 + lim_down, lim_up = ax.get_ylim() + ax.set_ylim(lim_down, lim_up + upper_limit_for_legend) # I could display the number of massif used to build each box plot. # plot_nb_massif_on_upper_axis(ax, labelsize, legend_fontsize, nb_massifs, x) @@ -211,7 +224,7 @@ def plot_shoe_plot_changes_against_altitude_for_maxima_and_total(massif_names, v start = 'Relative changes' if relative else 'Changes' unit = '\%' if relative else visualizer.study.variable_unit - ax.set_ylabel('{} between 1969 and 2019 ({})'.format(start, unit), + ax.set_ylabel('{} between 1959 and 2019 ({})'.format(start, unit), fontsize=legend_fontsize) ax.set_xlabel('Elevation', fontsize=legend_fontsize) ax.tick_params(axis='both', which='major', labelsize=labelsize) -- GitLab