I have to display some data, z = f(x,y,t), in a 3d bar-chart. I'd like to have this chart changing/animated with time.
For now, I can display my data z = f(x,y,t) for any given time t but I can't find a way to redraw the chart automatically to display the data of the next time step. Is there a way for doing this?
I tried with a simple loop but apparently I can only see the data for the last time step.
Here is the current version of my code:
from mpl_toolkits.mplot3d import Axes3D
from math import cos, sin
import matplotlib.pyplot as plt
import numpy as np
# An arbitraty function for z = f(x,t)
def z_xt(x, t):
return 30*sin(5*t) + x**2 + 20
# An arbitraty function for z = f(y,t)
def z_yt(y, t):
return 20*cos(2*t) + y**3/10 + 20
# Superposition z(x,y,t) = z(x,t) + z(y,t)
def z_xyt(f_xt, f_yt):
return f_xt+f_yt
# Definition of space and time domains
nx = 9; dx = 1
ny = 6; dy = 1
nt = 10; dt = 1
y, x = np.mgrid[slice(0, ny, dy),slice(0, nx, dx)]
t_list = [round(t*dt,2) for t in range(nt)]
# The matrix that contains the solution for every time step
Z_xyt = [z_xyt(z_xt(x, t), z_yt(y, t)) for t in t_list]
# Plot data in a 3D bar chart
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x_grid, y_grid = np.meshgrid(np.arange(nx), np.arange(ny))
x_grid = x_grid.flatten()
y_grid = y_grid.flatten()
# Iterate time and plot coresponding data
for index, t in enumerate(t_list):
# Z_xyt data at time t_list[index]
z_data = Z_xyt[index].flatten()
# Draw/actualize 3D bar chart data for every time step
bar_chart = ax.bar3d(x_grid, y_grid, np.zeros(len(z_data)), 1, 1, z_data)
plt.draw()
plt.show()
Thanks in advance!