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.
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.
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.
fig, ax = plt.subplots(figsize=(6, 6))
Creates a figure (fig) and an axis (ax) with a 6x6
aspect ratio for a square plot.
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.
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.
plt.show()
plt.show(): Displays the final gradient pattern.
0 Comments:
Post a Comment