sagemathinc / cocalc

CoCalc: Collaborative Calculation in the Cloud
https://CoCalc.com
Other
1.14k stars 207 forks source link

matplotlib animations evidently don't get rendered by default for some reason #7602

Closed williamstein closed 1 month ago

williamstein commented 1 month ago

Example

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

# Parameters
Lx, Ly = 10, 10     # Domain size
Nx, Ny = 100, 100   # Number of grid points
dx = Lx / (Nx - 1)  # Grid spacing x
dy = Ly / (Ny - 1)  # Grid spacing y
c = 1               # Wave speed
dt = 0.01           # Time step
Nt = 500            # Number of time steps

# Initialize grids
u = np.zeros((Nx, Ny))
u_new = np.zeros((Nx, Ny))
u_old = np.zeros((Nx, Ny))

# Initial condition (e.g., Gaussian)
x = np.linspace(0, Lx, Nx)
y = np.linspace(0, Ly, Ny)
X, Y = np.meshgrid(x, y)
u = np.exp(-((X - Lx/2)**2 + (Y - Ly/2)**2))

# Simulation loop
for n in range(Nt):
    for i in range(1, Nx-1):
        for j in range(1, Ny-1):
            u_new[i, j] = (2*u[i, j] - u_old[i, j] +
                           (c*dt/dx)**2 * (u[i+1, j] - 2*u[i, j] + u[i-1, j]) +
                           (c*dt/dy)**2 * (u[i, j+1] - 2*u[i, j] + u[i, j-1]))

    u_old = u.copy()
    u = u_new.copy()

# Plot the result
def update_plot(frame):
    plt.clf()
    plt.imshow(u, extent=[0, Lx, 0, Ly], origin='lower',
               cmap='viridis', vmin=-1, vmax=1)
    plt.colorbar()

fig = plt.figure()
ani = FuncAnimation(fig, update_plot, frames=Nt, repeat=False)
plt.show()
williamstein commented 1 month ago

I'm closing this, since it doesn't even display in JupyterLab either:

image