Thursday, 10 April 2025

Ocean Ripples Pattern using Python

 


import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

fig=plt.figure(figsize=(6,7))

ax=fig.add_subplot(111,projection='3d')

x=np.linspace(-10,10,200)

y=np.linspace(-10,10,200)

x,y=np.meshgrid(x,y)

r=np.sqrt(x**2+y**2)

z=np.sin(r)/r

z[r==0]=1

surf=ax.plot_surface(x,y,z,cmap='ocean',edgecolor='none')

ax.set_title('OceanRipple Pattern using Python',fontsize=16)

ax.set_xlabel('X Axis')

ax.set_xlabel('Y Axis')

ax.set_zlabel('Wave Height')

fig.colorbar(surf,shrink=0.5,aspect=5)

plt.tight_layout()

plt.show()

#source code --> clcoding.com 


Code Explanation:

1. Importing Libraries

import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

numpy is used for numerical operations, especially arrays and math functions.

matplotlib.pyplot is used for plotting graphs.

mpl_toolkits.mplot3d.Axes3D enables 3D plotting within matplotlib.

 

2. Creating the 3D Figure and Axes

fig = plt.figure(figsize=(10, 7))

ax = fig.add_subplot(111, projection='3d')

fig = plt.figure(figsize=(10, 7)): Initializes a new figure with size 10x7 inches.

add_subplot(111, projection='3d'): Adds a 3D subplot (1 row, 1 column, 1 plot) to the figure.

 

3. Generating a Grid for X and Y Coordinates

x = np.linspace(-10, 10, 200)

y = np.linspace(-10, 10, 200)

x, y = np.meshgrid(x, y)

np.linspace(-10, 10, 200): Creates 200 evenly spaced values between -10 and 10.

np.meshgrid(x, y): Converts the 1D arrays into 2D coordinate grids for X and Y, used for surface plotting.

 

4. Defining the Ocean Ripple Pattern (Z Values)

r = np.sqrt(x**2 + y**2)

z = np.sin(r) / r

z[r == 0] = 1

r = np.sqrt(x**2 + y**2): Computes the radial distance from the origin (like a circular ripple).

z = np.sin(r) / r: Defines a ripple pattern based on the sinc function, which gives a wave-like structure.

z[r == 0] = 1: Handles the case where r = 0 to avoid division by zero (since sin(0)/0 is undefined but limit = 1).

 

5. Plotting the Surface

surf = ax.plot_surface(x, y, z, cmap='ocean', edgecolor='none')

plot_surface: Plots a 3D surface based on X, Y, Z values.

cmap='ocean': Applies a blue-ish colormap to simulate water.

edgecolor='none': Removes gridlines for a smooth surface look.

 

6. Adding Title and Axis Labels

ax.set_title('OceanRipples pattern using python', fontsize=16)

ax.set_xlabel('X Axis')

ax.set_ylabel('Y Axis')

ax.set_zlabel('Wave Height')

Sets the main title and axis labels to describe the 3D plot.

 

7. Adding a Color Bar

fig.colorbar(surf, shrink=0.5, aspect=5)

Adds a color scale to the side of the plot to show what values each color represents.

shrink and aspect control its size and proportions.

 

8. Final Touch & Display

plt.tight_layout()

plt.show()

tight_layout(): Adjusts layout to avoid overlap.

show(): Displays the final plot.

 


Related Posts:

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (104) AI (41) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (200) C (77) C# (12) C++ (83) Course (67) Coursera (251) Cybersecurity (25) Data Analysis (3) Data Analytics (3) data management (11) Data Science (149) Data Strucures (8) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Events (6) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (11) Google (36) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (86) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1061) Python Coding Challenge (461) Python Quiz (134) Python Tips (5) Questions (2) R (70) React (6) Scripting (3) security (3) Selenium Webdriver (4) Software (17) SQL (42) UX Research (1) web application (8) Web development (4) web scraping (2)

Followers

Python Coding for Kids ( Free Demo for Everyone)