Commit 379d54a7 authored by Poulard Christine's avatar Poulard Christine :snake:
Browse files

Replace Chegodaiev.py

parent 0b16f67d
No related merge requests found
Showing with 71 additions and 23 deletions
+71 -23
...@@ -6,12 +6,23 @@ CPoulard Tchégodaiev / Hazen ...@@ -6,12 +6,23 @@ CPoulard Tchégodaiev / Hazen
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
from matplotlib.widgets import Slider, CheckButtons from matplotlib.widgets import Slider, CheckButtons
import numpy as np import numpy as np
from numpy.random import gumbel
a = 0.5
b = 0.5
N = 10 def echantillon_uniforme(nb):
echantillon = sorted(range(1, N+1), reverse=True) return sorted(range(1, N+1), reverse=True)
en_periode_de_retour = True
def echantillon_constant(nb):
# non encore testé
return [1]*nb
def echantillon_gumbel(nb):
# non encore testé
x0 = 196
gr = 102
return sorted(gumbel(loc=x0, scale=gr, size=nb))
def T_emp_Tchego(n_annees): def T_emp_Tchego(n_annees):
...@@ -26,11 +37,33 @@ def T_emp_parametre(n_annees, a , b): ...@@ -26,11 +37,33 @@ def T_emp_parametre(n_annees, a , b):
return plotting_positions_param return plotting_positions_param
def update_vlines(courbe_en_vlines, x, ymin=None, ymax=None):
# mise à jour de courbes de type vlines
# https://stackoverflow.com/questions/29331401/updating-pyplot-vlines-in-interactive-plot
seg_old = courbe_en_vlines.get_segments()
if ymin is None:
ymin = seg_old[0][0, 1]
if ymax is None:
ymax = seg_old[0][1, 1]
seg_new = [np.array([[xx, ymin],
[xx, ymax]]) for xx in x]
courbe_en_vlines.set_segments(seg_new)
def retracer_Tchego(echantillon): def retracer_Tchego(echantillon):
"""
on met à jour la courbe en vlines grâce à la fonction update_vlines ci-dessus
"""
y_max = max(echantillon)
T_emp = T_emp_Tchego(len(echantillon))
if en_periode_de_retour: if en_periode_de_retour:
courbe_estim_Tchego.set_data(T_emp_Tchego(len(echantillon)), echantillon) update_vlines(courbe_estim_Tchego, [T for T in T_emp], ymin=0, ymax=y_max)
else: else:
courbe_estim_Tchego.set_data([ 1 - (1 / T) for T in T_emp_Tchego(len(echantillon))], echantillon) update_vlines(courbe_estim_Tchego, [1 - (1 / T) for T in T_emp], ymin=0, ymax=y_max)
# pas d'appel à redessiner le canevas car si cette fontion est appelée elle est toujours suivie de retracer_parametree
def retracer_parametree(echantillon, a, b): def retracer_parametree(echantillon, a, b):
...@@ -64,10 +97,11 @@ def update_b(val): ...@@ -64,10 +97,11 @@ def update_b(val):
def update_N(val): def update_N(val):
global N, echantillon global N, echantillon
N = int( val) N = int( val) # ou bien N = slider_a.val
echantillon = sorted(range(1, N+1), reverse = True) echantillon = methode_echantillonnage(N)
retracer_parametree(echantillon, a, b)
retracer_Tchego(echantillon) retracer_Tchego(echantillon)
retracer_parametree(echantillon, a, b)
ax.set_ylim(bottom=0, top=N*1.1) ax.set_ylim(bottom=0, top=N*1.1)
fig_pp.canvas.draw_idle() fig_pp.canvas.draw_idle()
...@@ -83,6 +117,13 @@ def switch_freq(label): ...@@ -83,6 +117,13 @@ def switch_freq(label):
#plt.ion() #plt.ion()
a = 0.3
b = 0.4
N = 10
methode_echantillonnage = echantillon_uniforme
echantillon = methode_echantillonnage(N)
en_periode_de_retour = True
print(echantillon) print(echantillon)
print(a, b) print(a, b)
...@@ -91,12 +132,12 @@ fig_pp, (ax, ax_slide_a, ax_slide_b, ax_slide_n, ax_chb) = plt.subplots(nrows=5, ...@@ -91,12 +132,12 @@ fig_pp, (ax, ax_slide_a, ax_slide_b, ax_slide_n, ax_chb) = plt.subplots(nrows=5,
plt.subplots_adjust(wspace=1, hspace=0.5,left=0.1,top=0.85,right=0.9,bottom=0.1) plt.subplots_adjust(wspace=1, hspace=0.5,left=0.1,top=0.85,right=0.9,bottom=0.1)
fig_pp.canvas.set_window_title("Démo périodes de retour 'empiriques'") fig_pp.canvas.set_window_title("Démo périodes de retour 'empiriques'")
fig_pp.suptitle(f"les valeurs importent peu, seul leur rang est utilisé ! ") fig_pp.suptitle(f"seul le rang des observations est utilisé dans le calcul des T_empiriques ! ")
ax.set_xlabel("Période de retour (années)") ax.set_xlabel("Période de retour (années)")
courbe_estim_Tchego, = ax.plot(T_emp_Tchego(N), echantillon, color="red", alpha=0.7, linewidth=3, solid_capstyle='round', courbe_estim_Tchego = ax.vlines(x=T_emp_Tchego(N), ymin=0, ymax=max(echantillon), color="red", alpha=0.7, linewidth=2,
zorder=2, label= "Tchegodaiev", marker="o" , ls="--", markersize=7) zorder=2, label= "Tchegodaiev", ls="--")
courbe_estim_param, = ax.plot(T_emp_parametre(N, a, b), echantillon, color='blue', alpha=0.7, linewidth=5, solid_capstyle='round', courbe_estim_param, = ax.plot(T_emp_parametre(N, a, b), echantillon, color='blue', alpha=0.7, linewidth=5, solid_capstyle='round',
zorder=10, label = "paramétrée", marker="x" , ls=':', markersize=7) zorder=10, label = "paramétrée", marker="x" , ls=':', markersize=7)
...@@ -104,39 +145,46 @@ courbe_estim_param, = ax.plot(T_emp_parametre(N, a, b), echantillon, color='blu ...@@ -104,39 +145,46 @@ courbe_estim_param, = ax.plot(T_emp_parametre(N, a, b), echantillon, color='blu
for ax_s in [ax_slide_a, ax_slide_b, ax_slide_n, ax_chb]: for ax_s in [ax_slide_a, ax_slide_b, ax_slide_n, ax_chb]:
ax_s.xaxis.set_visible(False) ax_s.xaxis.set_visible(False)
ax_s.yaxis.set_visible(False) ax_s.yaxis.set_visible(False)
#for pos in ['right', 'top', 'bottom', 'left']:
# ax_s.spines[pos].set_visible(False) for pos in ['right', 'top', 'bottom', 'left']:
ax_chb.spines[pos].set_visible(False)
print("ici") print("ici")
# nom connu même hors de la fonction pour éviter le GC ? # nom connu même hors de la fonction pour éviter le GC ?
# a # a
slider_a = Slider( slider_a = Slider(
ax_slide_a, "paramètre a ", valmin=0.01, valmax = 0.99, valfmt='%0.2f', valinit=0.5, color="green") ax_slide_a, "paramètre a ", valmin=0.01, valmax = 0.99, valfmt='%0.2f', valinit=a, color="green")
slider_a.on_changed(update_a) slider_a.on_changed(update_a)
# b # b
slider_b = Slider( slider_b = Slider(
ax_slide_b, "paramètre b ", valmin=0.01, valmax = 0.99, valfmt='%0.2f', valinit=0.5, color="blue") ax_slide_b, "paramètre b ", valmin=0.01, valmax = 0.99, valfmt='%0.2f', valinit=b, color="blue")
slider_b.on_changed(update_b) slider_b.on_changed(update_b)
# N # N
valeurs_possibles_b = np.linspace(0, 100, 1) valeurs_possibles_N = np.linspace(0, 100, 1)
slider_n = Slider( slider_n = Slider(
ax_slide_n, "paramètre N ", valmin=5, valmax = 200, valfmt='%0.0f', valinit=N, valstep=valeurs_possibles_b, color="purple") ax_slide_n, "paramètre N ", valmin=5, valmax = 200, valfmt='%0.0f', valinit=N, valstep=valeurs_possibles_N, color="purple")
slider_n.on_changed(update_N) slider_n.on_changed(update_N)
# freq # switch T / freq
valeurs_possibles_b = np.linspace(0, 100, 1)
chb_enT = CheckButtons(ax_chb, [ "En période de retour (années)"], actives=[0]) chb_enT = CheckButtons(ax_chb, [ "Période de retour (années) ; sinon fréquence (de 0 à 1)"], [False])
"""
for rect in chb_enT.rectangles:
rect.set_width(0.1)
rect.set_height(1)
rect.xy = (0.05,0)
"""
chb_enT.on_clicked(switch_freq) chb_enT.on_clicked(switch_freq)
ax.legend(title="formules", bbox_to_anchor=(0.85, 0.10), loc='lower right') ax.legend(title="formules", bbox_to_anchor=(1.05, 0.10), loc='lower right')
#plt.tight_layout() #plt.tight_layout()
#fig_pp.savefig("ma_figure.png") #fig_pp.savefig("ma_figure.png")
#fig_pp.canvas.draw() #fig_pp.canvas.draw()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment