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.
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.
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.
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.
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.
plt.show()
Renders and displays the final
gradient-shaded wave pattern.
0 Comments:
Post a Comment