import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(-5,5,400)
y=np.linspace(-5,5,400)
X,Y=np.meshgrid(x,y)
R=np.sqrt(X**2+Y**2)
Z=np.sin(5+R)/(1+R)
plt.figure(figsize=(6,6))
plt.contourf(X,Y,Z,cmap='Blues')
plt.colorbar(label='Wave Intensity')
plt.axis('off')
plt.title('Water Ripples pattern')
plt.show()
#source code --> clcoding.com
Code Explanation:
Step 1: Importing Required Libraries
import numpy as np
import matplotlib.pyplot as plt
NumPy (np): Used for mathematical operations, especially creating a grid of points.
Matplotlib (plt): Used for visualization, specifically to create a contour plot of the ripple pattern.
Step 2: Creating a Grid of Points
x = np.linspace(-5, 5, 400) # X-axis range
y = np.linspace(-5, 5, 400) # Y-axis range
X, Y = np.meshgrid(x, y) # Create a coordinate grid
np.linspace(-5, 5, 400): Creates 400 evenly spaced points between -5 and 5 for both the X and Y axes.
np.meshgrid(x, y): Converts these 1D arrays into 2D coordinate matrices, so we can compute values at every point in a 2D space.
Step 3: Defining the Ripple Effect
R = np.sqrt(X**2 + Y**2) # Compute radial distance from the center
Z = np.sin(5 * R) / (1 + R) # Apply a damped sine wave function
R = np.sqrt(X**2 + Y**2):
Computes the radial distance of each point from the origin (0,0).
This ensures that waves radiate outward from the center.
Z = np.sin(5 * R) / (1 + R):
sin(5 * R): Generates oscillations that form the ripple pattern.
/(1 + R): Damps the waves so they fade out as they move away from the center.
Step 4: Plotting the Ripple Effect
plt.figure(figsize=(6, 6)) # Set figure size
plt.contourf(X, Y, Z, cmap='Blues') # Create filled contour plot
plt.colorbar(label='Wave Intensity') # Add color legend
plt.axis('off') # Remove axes for a clean look
plt.title("Water Ripple Pattern") # Set title
plt.show() # Display the plot
plt.contourf(X, Y, Z, cmap='Blues'):
Creates smooth color-filled contours based on Z values.
cmap='Blues': Uses blue shades to resemble water ripples.
plt.colorbar(label='Wave Intensity'):
Adds a color legend indicating the intensity of the waves.
plt.axis('off'):
Removes axes to give a clean water effect.
plt.show():
Displays the final ripple effect.
0 Comments:
Post a Comment