Saturday, 22 February 2025

Sandglass Pattern plot using python

 

import numpy as np

import matplotlib.pyplot as plt

x = np.linspace(-1, 1, 100)

y_upper = 1 - abs(x)  

y_lower = abs(x) - 1  

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

ax.fill_between(x, y_upper, 1, color="royalblue", alpha=0.7)  

ax.fill_between(x, y_lower, -1, color="tomato", alpha=0.7)  

ax.set_xlim(-1.2, 1.2)

ax.set_ylim(-1.2, 1.2)

ax.set_xticks([])

ax.set_yticks([])

ax.set_frame_on(False)

ax.axhline(0, color="black", linewidth=1.2, linestyle="--") 

plt.title("Hourglass Pattern Plot")

plt.show()

#source code --> clcoding.com 


Code Explanation:

Import Necessary Libraries

import numpy as np

import matplotlib.pyplot as plt

numpy is used for numerical operations, such as generating x values.

matplotlib.pyplot is used for visualization.


Define X-Coordinates for the Plot

x = np.linspace(-1, 1, 200)

np.linspace(-1, 1, 200):

Generates 200 evenly spaced values between -1 and 1 for smooth curves.

These values are used for defining the sandglass shape.


Define Y-Coordinates for the Upper and Lower Parts

y_upper = 1 - np.abs(x)  # Top inverted triangle

y_lower = np.abs(x) - 1  # Bottom triangle

y_upper = 1 - np.abs(x):

Defines an inverted triangle (top part of the sandglass).

As x moves from -1 to 1, y_upper decreases from 1 to 0.

y_lower = np.abs(x) - 1:

Defines a regular triangle (bottom part of the sandglass).

As x moves from -1 to 1, y_lower increases from -1 to 0.

Together, they form a symmetrical sandglass shape!


Create the Figure and Set Background

fig, ax = plt.subplots(figsize=(6, 8), facecolor="black")

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

Creates a taller figure (6x8) to emphasize the sandglass shape.

facecolor="black":

Sets the background color to black for better contrast.


Apply a Gradient Effect for Depth

for i in range(8):

    alpha = (8 - i) / 8  # Fading effect

    scale = 1 - i * 0.1  # Shrinking effect for layers

    ax.fill_between(x * scale, y_upper * scale, 1 * scale, color="deepskyblue", alpha=alpha)

    ax.fill_between(x * scale, y_lower * scale, -1 * scale, color="orangered", alpha=alpha)

A loop is used to create multiple layers to simulate a gradient depth effect.

alpha = (8 - i) / 8:

Decreases transparency from 1 to 0.1 as layers go deeper.

scale = 1 - i * 0.1:u

Shrinks each layer slightly to create a depth illusion.

fill_between() is used to fill the upper (blue) and lower (red) triangles.

This creates a smooth, glass-like fading effect, making the sandglass look more 3D.


Add a Dashed Symmetry Line

ax.axhline(0, color="white", linestyle="--", linewidth=1.2, alpha=0.6)

axhline(0): Draws a horizontal dashed line at y = 0.

This emphasizes symmetry and gives a structured appearance.


Remove Axis Details for a Clean Look

ax.set_xlim(-1.2, 1.2)

ax.set_ylim(-1.2, 1.2)

ax.set_xticks([])

ax.set_yticks([])

ax.set_frame_on(False)

set_xlim(-1.2, 1.2) / set_ylim(-1.2, 1.2):

Expands axes slightly for better visibility.

set_xticks([]) / set_yticks([]):

Removes axis labels to make the plot cleaner.

set_frame_on(False):

Removes the frame/border for a sleek look.


Add a Title and Show the Plot

ax.set_title("Sandglass Pattern Plot ", fontsize=14, fontweight="bold", color="white", pad=15)

plt.show()

Adds a bold, centered title with white color.

plt.show(): Displays the final sandglass plot.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (38) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (188) C (77) C# (12) C++ (83) Course (67) Coursera (247) Cybersecurity (25) Data Analysis (1) Data Analytics (2) data management (11) Data Science (142) Data Strucures (8) Deep Learning (21) Django (16) Downloads (3) edx (2) Engineering (14) Euron (29) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Generative AI (9) Google (34) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (76) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1006) Python Coding Challenge (452) Python Quiz (87) 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

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses

Python Coding for Kids ( Free Demo for Everyone)