Thursday, 27 March 2025

Mountain Horizon Pattern using Python

 


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().

 2. Parameters and Initialization

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.

 3. Creating the Mountain Layers

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.

 4. Adding Color with Transparency

color = (0.2, 0.3, 0.5, 1 - i / (num_layers + 1))

Color: Uses an RGBA value where:

 Red (0.2), Green (0.3), and Blue (0.5) create a cool blue tone, like distant mountains.

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.

 5. Plotting the Mountains

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.

 6. Adjusting the Plot

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

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 (1042) Python Coding Challenge (456) Python Quiz (117) 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)