Bonjour à tous,
j'utilise matplotlib pour dessiner la marche aléatoire d'un robot, un exercice niveau terminale dans un chapitre Probabilités.
J'ai écrit le programme ci-dessous qui fonctionne correctement quand je réalise la marche avec 100 déplacements, avec un nombre de déplacements égal à 24*60*60 (1 saut par seconde pedant une journée), le programme bloque par une limite de capacité que je ne sais pas traiter.
Voici le programme que j'ai écrit avec Thonny :
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
from random import randint
 
def deplacement(n):
    abscisse=[0];ordonnée=[0]
    t=randint(1,4)
    x=abscisse[0];y=ordonnée[0]
    for i in range(n):
        t=randint(1,4)
        if t==1:
            x=x+1
        elif t==2:
            y=y+1
        elif t==3:
            x=x-1
        else:
            y=y-1
        abscisse.append(x)
        ordonnée.append(y)       
    return abscisse, ordonnée
 
n=24*60*60
X,Y=deplacement(n)
#print(X,Y)
import matplotlib.pyplot as plt
plt.figure(figsize=(100,100))
plt.axis('on')
plt.plot([0],[0],marker='o',color='red')
for i in range(n):
#    print(i,X[i],Y[i],X[i+1],Y[i+1])
    plt.plot(X,Y,marker=' ',color='blue')
plt.plot([X[n]],[Y[n]],marker='o',color='green')
plt.title('Marche aléatoire')
plt.grid()
plt.show()
J'ai regardé les paramètres de figure sans bien comprendre comment faire pour afficher la figure, par plusieurs essais j'ai obtenu des maximums pour X ou X de l'ordre de 1.000, cette valeur ne va pas dans figure.
Ma question un peu naïve ; Quelle est l'astuce pour sortir la figure avec autant de déplacements?
En remerciant d'avance pour l'aide apportée, bonne journée à tous.

Voici la console Python après exécution du programme :
>>> %Run TP-002-p286-Une-seconde-marche-aleatoire.py
ERROR thonny.backend: PROBLEM WITH THONNY'S BACK-END
Traceback (most recent call last):
File "C:\Users\pzorba75\AppData\Local\Programs\Thonny\lib\site-packages\thonny\plugins\cpython\cpython_backend.py", line 1238, in wrapper
result = method(self, *args, **kwargs)
File "C:\Users\pzorba75\AppData\Local\Programs\Thonny\lib\site-packages\thonny\plugins\cpython\cpython_backend.py", line 1225, in wrapper
return method(self, *args, **kwargs)
File "C:\Users\pzorba75\AppData\Local\Programs\Thonny\lib\site-packages\thonny\plugins\cpython\cpython_backend.py", line 1295, in _execute_prepared_user_code
exec(statements, global_vars)
File "C:\Users\pzorba75\Documents\M-Mathématiques\1-Spé\Barbazo\Chap-09-Probabilités-conditionnelles\TP-002-p286-Une-seconde-marche-aleatoire.py", line 30, in <module>
plt.plot(X,Y,marker=' ',color='blue')
File "C:\Users\pzorba75\AppData\Roaming\Python\Python37\site-packages\matplotlib\pyplot.py", line 2769, in plot
**({"data": data} if data is not None else {}), **kwargs)
File "C:\Users\pzorba75\AppData\Roaming\Python\Python37\site-packages\matplotlib\axes\_axes.py", line 1637, in plot
self.add_line(line)
File "C:\Users\pzorba75\AppData\Roaming\Python\Python37\site-packages\matplotlib\axes\_base.py", line 2288, in add_line
self._update_line_limits(line)
File "C:\Users\pzorba75\AppData\Roaming\Python\Python37\site-packages\matplotlib\axes\_base.py", line 2311, in _update_line_limits
path = line.get_path()
File "C:\Users\pzorba75\AppData\Roaming\Python\Python37\site-packages\matplotlib\lines.py", line 999, in get_path
self.recache()
File "C:\Users\pzorba75\AppData\Roaming\Python\Python37\site-packages\matplotlib\lines.py", line 661, in recache
self._xy = np.column_stack(np.broadcast_arrays(x, y)).astype(float)
File "<__array_function__ internals>", line 6, in column_stack
File "C:\Users\pzorba75\AppData\Roaming\Python\Python37\site-packages\numpy\lib\shape_base.py", line 656, in column_stack
return _nx.concatenate(arrays, 1)
File "<__array_function__ internals>", line 6, in concatenate
numpy.core._exceptions._ArrayMemoryError: Unable to allocate 1.32 MiB for an array with shape (86401, 2) and data type float64

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\pzorba75\AppData\Local\Programs\Thonny\lib\site-packages\thonny\plugins\cpython\cpython_backend.py", line 1283, in execute_source
return self._execute_prepared_user_code(statements, global_vars)
File "C:\Users\pzorba75\AppData\Local\Programs\Thonny\lib\site-packages\thonny\plugins\cpython\cpython_backend.py", line 1243, in wrapper
return {"user_exception": self._backend._prepare_user_exception()}
File "C:\Users\pzorba75\AppData\Local\Programs\Thonny\lib\site-packages\thonny\plugins\cpython\cpython_backend.py", line 1076, in _prepare_user_exception
"stack": self._export_stack(last_frame),
File "C:\Users\pzorba75\AppData\Local\Programs\Thonny\lib\site-packages\thonny\plugins\cpython\cpython_backend.py", line 968, in _export_stack
module_name = system_frame.f_globals["__name__"]
KeyError: '__name__'
>>>