Skip to content
Snippets Groups Projects
Commit 7f83cc0f authored by Bonnemains Matthieu's avatar Bonnemains Matthieu
Browse files

Delete test.py

parent b286ab78
No related branches found
No related tags found
No related merge requests found
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.mplot3d import Axes3D
# Function to visualize
def function_to_plot(x, y):
return (x**2 + y**2) * np.sin(np.sqrt(x**2 + y**2)) # Example function with minima
# 3D Plot Function
def plot_3D(populations, function_to_plot, save_path="animation.gif"):
fig = plt.figure(figsize=(10, 7))
ax = fig.add_subplot(111, projection='3d')
# Generate grid for the surface plot
x = np.linspace(-100, 100, 200)
y = np.linspace(-100, 100, 200)
X, Y = np.meshgrid(x, y)
Z = function_to_plot(X, Y)
# Plot surface
ax.plot_surface(X, Y, Z, cmap="viridis", alpha=0.6, edgecolor='none')
# Initialize scatter plot for moving points
scatter = ax.scatter([], [], [], color='red', s=1, alpha=0.8)
# Update function for animation
def update(frame):
ax.collections.clear() # Clear previous points
points = populations[frame] # Get current positions
Z_points = function_to_plot(points[:, 0], points[:, 1]) # Compute function values
scatter = ax.scatter(points[:, 0], points[:, 1], Z_points, color='red', s=1, alpha=0.8)
return scatter,
# Create animation
ani = animation.FuncAnimation(fig, update, frames=len(populations), interval=100, blit=False)
# Save animation as GIF
ani.save(save_path, writer='pillow', fps=20)
plt.show()
# Example Usage:
populations = [np.random.uniform(-100, 100, size=(20000, 2)) for _ in range(50)] # Fake data
plot_3D(populations, function_to_plot)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment