Jan-09-2020, 08:25 PM
Hi, i am having an issue coding a 3D matplotlib animation, my Z axis that I'd like to lock from 0 to 50 is changing with the animation, here is the code :
import numpy as np
from random import *
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib import animation
from matplotlib.ticker import LinearLocator, FormatStrFormatter
T = np.zeros((30,30))
S = 3
# Détection des éléments trop hauts du tas de sable
def verif(t, L) :
i = 0
while i < len(L) and t - L[i] < S :
i += 1
return(i < len(L))
#
def choix(t, L) :
ch = 0
for k in range(len(L)) :
if t - L[k] >= S :
if L[k] < L[ch] :
ch = k
elif L[k] == L[ch] :
ch = choice([k,ch])
return(ch)
#
def trouve(n, i, j) :
x, y = 0, 0
if n == 0 :
x, y = i + 1, j - 1
elif n == 1 :
x, y = i + 1, j
elif n == 2 :
x, y = i + 1, j + 1
elif n == 3 :
x, y = i, j - 1
elif n == 4 :
x, y = i, j + 1
elif n == 5 :
x, y = i - 1, j - 1
elif n == 6 :
x, y = i - 1, j
elif n == 7 :
x, y = i - 1, j + 1
return(x, y)
# Fonctions réalisant les avalanches dans le quadrant haut/gauche
def chute1(T) :
(n, p) = np.shape(T)
# On ajoute aléatoirement de 1 à 4 grains au tas de sable
T[(n // 2) - 1, (p // 2) - 1] += randint(0, 1)
T[(n // 2) - 1, (p // 2)] += randint(0, 1)
T[(n // 2), (p // 2) - 1] += randint(0, 1)
T[(n // 2), (p // 2)] += randint(0, 1)
for i in range(n // 2 + 3, 2, -1) :
for j in range((p // 2) + 3, 2, -1) :
if verif(T[i,j], [T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]]) == True :
lst = [T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]]
c = choix(T[i,j],[T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]])
h = lst[c]
d = T[i,j] - h
b = randint(0, d)
x,y = trouve(c, i, j)
T[x,y] += b
T[i,j] -= b
return(T)
def chute2(T) :
(n,p) = np.shape(T)
for j in range((p // 2) + 3, 2, -1) :
for i in range((n // 2) - 3, 2, -1) :
if verif(T[i,j], [T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]]) == True :
lst = [T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]]
c = choix(T[i,j],[T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]])
h = lst[c]
d = T[i,j] - h
b = randint(0,d)
x,y = trouve(c, i, j)
T[x,y] += b
T[i,j] -= b
return(T)
# Fonctions réalisant les avalanches dans le quadrant bas/gauche
def chute3(T):
(n,p) = np.shape(T)
for i in range((n // 2) - 3, (n - 2)) :
for j in range((p // 2) + 3, 2, -1) :
if verif(T[i,j], [T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]]) == True :
lst = [T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]]
c = choix(T[i,j],[T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]])
h = lst[c]
d = T[i,j] - h
b = randint(0,d)
x,y = trouve(c,i,j)
T[x,y] += b
T[i,j] -= b
return(T)
def chute4(T) :
(n,p) = np.shape(T)
for j in range((p // 2) + 3, 2, -1) :
for i in range((n // 2) - 3, (n - 2)) :
if verif(T[i,j], [T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]]) == True :
lst = [T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]]
c = choix(T[i,j],[T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]])
h = lst[c]
d = T[i,j] - h
b = randint(0,d)
x,y = trouve(c,i,j)
T[x,y] += b
T[i,j] -= b
return(T)
# Fonctions réalisant les avalanches dans le quadrant haut/droit
def chute5(T) :
(n,p) = np.shape(T)
for i in range((n // 2) + 3, 2, -1) :
for j in range((p // 2) - 3, (p - 2)) :
if verif(T[i,j], [T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]]) == True :
lst = [T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]]
c = choix(T[i,j],[T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]])
h = lst[c]
d = T[i,j] - h
b = randint(0,d)
x,y = trouve(c,i,j)
T[x,y] += b
T[i,j] -= b
return(T)
def chute6(T) :
(n,p) = np.shape(T)
for j in range((p // 2) - 3, (p - 2)) :
for i in range((n // 2) + 3, 2, -1) :
if verif(T[i,j], [T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]]) == True :
lst = [T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]]
c = choix(T[i,j],[T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]])
h = lst[c]
d = T[i,j] - h
b = randint(0,d)
x,y = trouve(c,i,j)
T[x,y] += b
T[i,j] -= b
return(T)
# Fonctions réalisant les avalanches dans le quadrant bas/droit
def chute7(T) :
(n,p) = np.shape(T)
for i in range((n // 2) - 3, (n - 3)) :
for j in range((p // 2) - 3, (p - 2)) :
if verif(T[i,j], [T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]]) == True :
lst = [T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]]
c = choix(T[i,j],[T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]])
h = lst[c]
d = T[i,j] - h
b = randint(0,d)
x,y = trouve(c,i,j)
T[x,y] += b
T[i,j] -= b
return(T)
def chute8(T) :
(n,p) = np.shape(T)
for j in range((p // 2) - 3, (p - 2)) :
for i in range((n // 2) - 3, (n - 2)) :
if verif(T[i,j], [T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]]) == True :
lst = [T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]]
c = choix(T[i,j],[T[i+1,j-1],T[i+1,j],T[i+1,j+1],T[i,j-1],T[i,j+1],T[i-1,j-1],T[i-1,j],T[i-1,j+1]])
h = lst[c]
d = T[i,j] - h
b = randint(0,d)
x,y = trouve(c,i,j)
T[x,y] += b
T[i,j] -= b
return(T)
# Ici toutes les avalanches sont regroupées dans une seule fonction
def chute(T) :
chute1(T),chute2(T),chute3(T),chute4(T),chute5(T),chute6(T),chute7(T),chute8(T)
return T
def animate(i, Z, surf):
Z = chute(T)
surf = ax.plot_surface(X, Y, Z, color= 'beige')
return surf,
fig = plt.figure()
X = np.linspace(0, 29, 30)
Y = np.linspace(0, 29, 30)
X ,Y = np.meshgrid(X, Y)
Z = T
surf = ax.plot_surface(X, Y, Z,color= 'beige')
ax = fig.add_subplot(111, projection='3d')
ani = animation.FuncAnimation(fig, animate, fargs = (Z, surf), interval = 100,frames = 200, repeat = False, blit=False)
plt.show()The code might not be really efficient but what bothers me is that I cannot manage to lock Z axis even with a init_func for the animation. Thanks for your help if anyone can help
