Bonjour,
J' ai écrit un programme sencé me renvoyer une figure en fonction de la dimension choisie.
Quand vous lancez l'algo, une fenetre s'affiche et demande de choisir le nombre de pas pour ma marche aléatoire puis la dimension.

En cliquant sur la dimension 1, la figure s'affiche (c'est bon). En cliquant sur la dimension 2 une animation devrait etre renvoyée mais ce n'est pas le cas. Ce que je ne comprends pas c'est que les valeurs N, traj_x et traj_y sont lus et correctes (le print me le confirme) et la classe Creation2d est sencé être juste (lorsque je la lance seule dans un autre programme 'le programme essai_marche2', sans l'intermédiaire de la classe Choix, elle me renvoie l'animation que je souhaite.

(oubliez la dimension 3, ce sera pour plus tard).

Voici mon programme avec la premiere fenetre Tkinter qui ne fonctionne pas (pour la dimension 2)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*- 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()
Et le programme pour la dimension 2 sans la fenetre Tkinter qui fonctionne
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 22 11:04:15 2017
 
@author: celin
"""
 
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
 
class UpdateDist(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('Animation: Recuit simulé, cas simple')
 
    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
 
fig, ax = plt.subplots()
N=50
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
 
X = np.cumsum(pas_x)
Y = np.cumsum(pas_y)
ud = UpdateDist(ax,X,Y)
anim = FuncAnimation(fig, ud.ani, frames=np.arange(N), init_func=ud.ani_init,
                     interval=20, repeat=False)
plt.show()