# -*- coding: utf-8 -*- """ Created on Sun Jan 15 15:54:44 2017 @author: celin """ import numpy as np from matplotlib import pyplot as plt import matplotlib.animation as animation import tkinter as tk ######################Marches aléatoires################################ ####Initialisation de quelques données utiles #### #(variables globales, lues par toutes les classes) N = 0 ####Définition des paramètres en fonction du choix#### class Choix(tk.Tk): #Création des objets utilisés dans cette classe (méthode appelée constructeur) def __init__(self, *args, **kwargs): #la fenêtre tk.Tk.__init__(self, *args, **kwargs) tk.Tk.wm_title(self, "Choix") #Les boutons txt = tk.Label(self, text = 'Nombre de Pas',height=2, width=27) txt.grid(row=0, column=0) button00 = tk.Button(self, text='N = 100', command=(lambda: self.valeur_N(100))) button00.grid(row=1, column=0) button01 = tk.Button(self, text='N = 500', command=(lambda: self.valeur_N(500))) button01.grid(row=2, column=0) button02 = tk.Button(self, text='N = 1000', command=(lambda: self.valeur_N(1000))) button02.grid(row=3, column=0) txt = tk.Label(self, text = 'Dimension',height=3, width=25) txt.grid(row=4, column=0) button10 = tk.Button(self, text='d = 1', command=(lambda: self.affichage(1))) button10.grid(row=5, column=0) button11 = tk.Button(self, text='d = 2', command=(lambda: self.affichage(2))) button11.grid(row=6, column=0) button12 = tk.Button(self, text='d = 3', command=(lambda: self.affichage(3))) button12.grid(row=7, column=0) button = tk.Button(self, text='Quitter',height=2, width=10, command=self.destroy) button.grid(row=8, column=0) #Le choix du nombre de Pas fait appelle à cette fonction def valeur_N(self,choix_N): global N N = choix_N #Le choix de la dimension fait appelle à celle là qui lance la figure def affichage(self, d): if d == 1 : traj = np.insert(np.cumsum( (np.random.rand(N) < (1/2))*2 - 1 ),0,0) x = np.arange(N+1) plt.close() plt.title('Marche aléatoire sur Z entre 0 et N={}'.format(N)) plt.plot(x,traj) plt.show() elif d == 2 : #calcul de la trajectoire alea_marche1 = (np.random.rand(N) < (1/2))*1 #choix x ou y alea_marche2 = (np.random.rand(N) < (1/2))*2 - 1 #choix 1 ou -1 pas_x = alea_marche1*alea_marche2 pas_y = (1 - alea_marche1)*alea_marche2 traj_x = np.cumsum(pas_x) traj_y = np.cumsum(pas_y) print(traj_x,traj_y,N) #figure plt.close() fig, ax = plt.subplots() c = Creation2d(ax,traj_x,traj_y) animation.FuncAnimation(fig, c.ani, frames=np.arange(N), init_func=c.ani_init, interval=20, repeat=False) plt.show() else : print(d,N) #calcul x, y, z # Creation3d(x,y,z,N,self.container, self) class Creation2d(object): def __init__(self, ax, X, Y): self.line, = ax.plot([],[],'k-') self.x = [0] self.y = [0] self.X = X self.Y = Y #affichage Rmax_x = np.max(np.abs(X)) Rmax_y = np.max(np.abs(Y)) ax.set_xlim(-Rmax_x, Rmax_x) ax.set_ylim(-Rmax_y, Rmax_y) ax.set_title('Marche aléatoire sur Z^2 entre 0 et N={}'.format(N)) def ani_init(self): self.line.set_data([], []) return self.line, def ani(self,i): #réalisation de l'animation self.x += [self.X[i]] self.y += [self.Y[i]] self.line.set_data(self.x, self.y) return self.line fenetre = Choix() fenetre.geometry("200x300") fenetre.mainloop()