import numpy as np
import matplotlib.pyplot as plt
num_layers = 7
x = np.linspace(0, 10, 500)
plt.figure(figsize=(8, 6))
for i in range(num_layers):
y = 1.5 * np.sin(x + i * 0.5) + np.random.uniform(-0.2, 0.2, size=x.shape) + (i * 0.8)
color = (0.2, 0.3, 0.5, 1 - i / (num_layers + 1))
plt.fill_between(x, y, -5, color=color)
plt.axis('off')
plt.gca().set_aspect('auto', adjustable='box')
plt.title('Mountain Horizon Pattern', fontsize=14)
plt.show()
#source code --> clcoding.com
Code Explanation:
1. Importing Libraries
import numpy as np
import matplotlib.pyplot as plt
NumPy: Used for mathematical operations and to
generate smooth x-values (np.linspace) and random noise (np.random.uniform).
Matplotlib: Used to plot and visualize the mountains
using plt.fill_between().
num_layers = 7
x = np.linspace(0, 10, 500)
plt.figure(figsize=(10, 6))
num_layers = 7: The number of mountain layers to be
drawn, creating a sense of depth.
x = np.linspace(0, 10, 500):
Generates 500 points between 0 and 10 for the
x-axis, ensuring smooth curves.
plt.figure(figsize=(10, 6)):
Creates a figure with a size of 10x6 inches,
providing a wide, scenic view.
for i in range(num_layers):
y = 1.5 *
np.sin(x + i * 0.5) + np.random.uniform(-0.2, 0.2, size=x.shape) + (i * 0.8)
for i in range(num_layers): A loop that generates
each layer of mountains.
np.sin(x + i * 0.5):
A sine wave creates the smooth, wavy shape of the
mountains.
The i * 0.5 adds a phase shift to differentiate the
layers.
np.random.uniform(-0.2, 0.2, size=x.shape):
Adds small random variations (noise) to the wave,
making the mountains look more natural and rugged.
(i * 0.8):
Gradually raises each subsequent layer, simulating
mountains in the distance.
color = (0.2, 0.3, 0.5, 1 - i / (num_layers + 1))
Color: Uses an RGBA value where:
Alpha (1 - i / (num_layers + 1)) controls
transparency.
Closer mountains are darker and more opaque.
Distant mountains are lighter and more transparent,
creating an atmospheric perspective.
plt.fill_between(x, y, -5, color=color)
plt.fill_between():
Fills the area between the mountain curve (y) and
the bottom of the plot (-5).
This creates the solid mountain silhouette.
plt.axis('off')
plt.gca().set_aspect('auto', adjustable='box')
plt.title('Mountain Horizon Pattern', fontsize=14)
plt.show()
plt.axis('off'): Hides the axis for a clean,
artistic look.
plt.gca().set_aspect('auto', adjustable='box'):
Ensures the aspect ratio adjusts naturally to fit
the plot.
plt.title(): Adds a descriptive title.
plt.show(): Displays the final output.
0 Comments:
Post a Comment