Python Forum
Changing axis graduation matplotlib 3D animation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing axis graduation matplotlib 3D animation
#1
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 Smile Smile Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  matplotlib x-axis text move bottom upward jacklee26 3 930 May-31-2023, 04:28 AM
Last Post: jacklee26
  x-axis labels with Matplotlib Mark17 8 2,124 Mar-23-2022, 06:10 PM
Last Post: Mark17
  How to make x-axis readable with matplotlib Mark17 7 3,820 Mar-01-2022, 04:30 PM
Last Post: DPaul
  matplotlib x axis range goes over the set range Pedroski55 5 3,111 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Matplotlib Animation with Threading peterjv26 4 7,016 Oct-08-2021, 05:51 PM
Last Post: peterjv26
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 4,233 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  Graphics Formatting - X-axis Notation and Annotations - Matplotlib silviover_junior 0 1,759 Mar-17-2021, 01:19 PM
Last Post: silviover_junior
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,162 Mar-11-2021, 10:52 AM
Last Post: buran
  matplotlib x-axis wrong order SchroedingersLion 4 4,180 Feb-23-2021, 05:42 PM
Last Post: nilamo
  cannot create animation on 2D array using Matplotlib and FuncAnimation Caffeine_Addict 1 2,461 Jan-12-2021, 11:35 AM
Last Post: Caffeine_Addict

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020