Matplotlib animation

Bonjour
Dans le code suivant, je stocke des histogrammes et des plots pour les afficher. Je n'obtiens pas d'animation, je ne comprends pas pourquoi. Avez-vous une idée ?
import numpy as np
import scipy.stats as st

%matplotlib notebook 
import matplotlib.pyplot as plt
import matplotlib.animation as animation

def Metropolis(pi,Q,tps_max,X0, display):
    """
    d
    """
    X = np.zeros(tps_max)
    X[0] = X0
    
    if display:
        ims=[] #  list of images for animation
        fig,axs = plt.subplots(1,2)
    
    for tps in range(1,tps_max):
        y = np.random.rand() # Tiré y selon Q(Xn,y)
        U = np.random.rand()# Tirer U hasard sur [0,1]
        if U < np.min( np.array([pi(y)/pi(X[tps-1]), 1])) :# Si U < R(Xn,y) accepter la sélection
            X[tps] = y
        else:
            X[tps] = X[tps-1] # Sinon refuser la sélection (Rabiot)
        if tps %100 == 0:
            print('Etape {}/{}'.format(tps,tps_max))
        
        if display:
            im1 = axs[0].hist(X)
            im2, = axs[1].plot(np.arange(tps),[np.sum(X[:t])/(t+1) for t in range(tps)])
            ims.append([im1, im2])
    
    if display:
        ani = animation.ArtistAnimation(fig,ims, interval=50,blit=False)
        plt.show()
    
    return X
EDIT : le
%matplotlib notebook
vient du fait que je souhaite faire une animation sur Jupyter.

Réponses

  • Pourtant le code suivant fonctionne très bien :
    #create image with format (time,x,y)
    image = np.random.rand(10,10,10)
    image2 = np.random.rand(10,10,10)
    
    #setup figure
    fig,axs = plt.subplots(1,2, figsize = [6,6])
    ax1=axs[0]
    ax2=axs[1]
    
    #set up list of images for animation
    ims=[]
    for time in range(np.shape(image)[1]):
        im = ax1.imshow(image[time,:,:])
        im2, = ax2.plot(image[0:time,5,5])
    
        ims.append([im, im2])
    
    #run animation
    ani = animation.ArtistAnimation(fig,ims, interval=50,blit=False)
    plt.show()
    

    avec les même import que celui d'avant. Ceci me laisse penser que tout devrait fonctionner.
Connectez-vous ou Inscrivez-vous pour répondre.