Tuesday, 25 March 2025

Noise Based Gradient Pattern using Python

 



import numpy as np

import matplotlib.pyplot as plt

from scipy.ndimage  import gaussian_filter

np.random.seed(42)

noise=np.random.rand(100,100)

smooth_noise=gaussian_filter(noise,sigma=10)

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

ax.imshow(smooth_noise,cmap='viridis',interpolation='bilinear')

ax.set_xticks([])

ax.set_yticks([])

ax.set_frame_on(False)

plt.title("Noise based gradient pattern")

plt.show()

#source code --> clcoding.com

Code Explanation:

1. Import Required Libraries

import numpy as np

import matplotlib.pyplot as plt

from scipy.ndimage import gaussian_filter

numpy: Used to generate random noise.

matplotlib.pyplot: Used to display the pattern.

gaussian_filter: Applies a blur effect to smooth the noise into a gradient.

 2. Generate Random Noise

np.random.seed(42)  # For reproducibility

noise = np.random.rand(100, 100)  # 100x100 grid of random values

np.random.seed(42): Ensures that the random values are consistent each time the code runs.

np.random.rand(100, 100): Creates a 100×100 grid of random values between 0 and 1, forming a random noise pattern.

 3. Apply Gaussian Blur to Create Gradient Effect

smooth_noise = gaussian_filter(noise, sigma=10)  # Adjust sigma for smoothness

gaussian_filter(): Applies a Gaussian blur to smooth out the noise.

sigma=10: Controls the amount of blurring.

Higher values (e.g., sigma=20) → Smoother, more blended gradient.

Lower values (e.g., sigma=5) → Sharper, more detailed noise.

 4. Create the Figure and Axis

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

Creates a figure (fig) and an axis (ax) with a 6x6 aspect ratio for a square plot.

 5. Display the Gradient Pattern

ax.imshow(smooth_noise, cmap='viridis', interpolation='bilinear')

ax.imshow(smooth_noise): Displays the smoothed noise as an image.

cmap='viridis': Uses the "viridis" colormap for a smooth color transition.

Other options: "plasma", "magma", "gray", etc.

interpolation='bilinear': Ensures smooth transitions between pixels.

 6. Remove Axes for a Clean Look

ax.set_xticks([])

ax.set_yticks([])

ax.set_frame_on(False)

ax.set_xticks([]) and ax.set_yticks([]): Hides axis labels for a cleaner visualization.

ax.set_frame_on(False): Removes the surrounding box, making the gradient the main focus.

 7. Display the Pattern

plt.show()

plt.show(): Displays the final gradient 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)