Tuesday, 25 March 2025

Gradient Wave Pattern Using Python

 

import matplotlib.pyplot as plt

import numpy as np

x=np.linspace(0,10,500)

y_base=np.sin(x)

fig,ax=plt.subplots(figsize=(8,6))

for i in range(10):

    y_offset=i*0.1

    alpha=1-i*0.1

    ax.fill_between(x,y_base-y_offset,y_base+y_offset,color='royalblue',alpha=alpha)

ax.set_xticks([])

ax.set_yticks([])

ax.set_xlim(0,10)

ax.set_ylim(-2,2)

plt.title("Gradient wave pattern")

plt.show()

#source code --> clcoding.com

Code Explanation:

1. Import Required Libraries

import numpy as np

import matplotlib.pyplot as plt

numpy (np): Used for numerical operations, especially to generate x-values.

matplotlib.pyplot (plt): Used for plotting the wave patterns.

 2. Define the Wave Parameters

x = np.linspace(0, 10, 500)  # X-axis range

y_base = np.sin(x)  # Base sine wave

np.linspace(0, 10, 500): Creates 500 evenly spaced points between 0 and 10 (smooth x-axis).

np.sin(x): Generates a sine wave for a smooth oscillating wave pattern.

 3. Create a Gradient Effect by Stacking Multiple Waves

fig, ax = plt.subplots(figsize=(8, 6))

plt.subplots(figsize=(8, 6)): Creates a figure (fig) and an axis (ax) with an 8×6-inch size.

 or i in range(10):  # 10 overlapping waves

    y_offset = i * 0.1  # Offset to create depth effect

    alpha = 1 - i * 0.1  # Decreasing opacity for fading effect

    ax.fill_between(x, y_base - y_offset, y_base + y_offset, color='royalblue', alpha=alpha)

Loops through 10 layers to create a stacked wave effect.

y_offset = i * 0.1: Shifts each wave slightly downward to create depth.

alpha = 1 - i * 0.1: Controls transparency (1 = opaque, 0 = invisible), making waves fade gradually.

fill_between(x, y_base - y_offset, y_base + y_offset):

Fills the area between two curves.

Creates the gradient shading effect.

 4. Formatting the Plot

ax.set_xticks([])  # Remove x-axis ticks

ax.set_yticks([])  # Remove y-axis ticks

ax.set_frame_on(False)  # Remove the plot border

ax.set_xlim(0, 10)  # X-axis range

ax.set_ylim(-2, 2)  # Y-axis range

Removes ticks and frames for a clean, artistic appearance.

Sets axis limits so that the wave pattern is well-contained.

 5. Display the Plot

plt.show()

Renders and displays the final gradient-shaded wave pattern.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (39) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (197) C (77) C# (12) C++ (83) Course (67) Coursera (249) Cybersecurity (25) Data Analysis (2) Data Analytics (2) data management (11) Data Science (148) 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 (85) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1041) Python Coding Challenge (454) Python Quiz (116) Python Tips (5) Questions (2) R (70) React (6) Scripting (1) 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)