# -*- 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()