Sunday, 23 February 2025

Hourglass 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 to generate numerical data (e.g., linspace for smooth curves).
matplotlib.pyplot is used for visualization.

Define X-Coordinates for the Plot
x = np.linspace(-1, 1, 100)
np.linspace(-1, 1, 100): Generates 100 evenly spaced points between -1 and 1.
These x values will be used to define the hourglass shape.

Define Y-Coordinates for Upper and Lower Triangles
y_upper = 1 - abs(x)  
y_lower = abs(x) - 1  
y_upper = 1 - abs(x):
This represents the upper inverted triangle.
As x moves from -1 to 1, the y values decrease from 1 to 0 symmetrically.
y_lower = abs(x) - 1:
This represents the lower triangle.
As x moves from -1 to 1, the y values increase from -1 to 0 symmetrically.
Together, they form an hourglass shape!

Create the Plot
fig, ax = plt.subplots(figsize=(6, 6))
fig, ax = plt.subplots(figsize=(6, 6)):
Creates a square-shaped figure of size 6x6.

Fill the Upper and Lower Triangles
ax.fill_between(x, y_upper, 1, color="royalblue", alpha=0.7)  
ax.fill_between(x, y_lower, -1, color="tomato", alpha=0.7)
fill_between(x, y_upper, 1, color="royalblue", alpha=0.7):
Fills the area between y_upper and 1 with blue.
fill_between(x, y_lower, -1, color="tomato", alpha=0.7):
Fills the area between y_lower and -1 with red.
alpha=0.7:
Transparency of 70% (makes colors blend better).

Adjust Axis Limits and Appearance
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.set_xlim(-1.2, 1.2): Expands x-axis slightly beyond -1 and 1.
ax.set_ylim(-1.2, 1.2): Expands y-axis slightly beyond -1 and 1.
ax.set_xticks([]): Removes x-axis tick marks.
ax.set_yticks([]): Removes y-axis tick marks.
ax.set_frame_on(False): Removes the border around the plot.

Add a Symmetry Line
ax.axhline(0, color="black", linewidth=1.2, linestyle="--")
axhline(0, color="black", linewidth=1.2, linestyle="--"):
Draws a dashed black line at y = 0 to emphasize symmetry.

Add Title and Display Plot
ax.set_title("Hourglass Pattern Plot", fontsize=14, fontweight="bold")
plt.show()
set_title("Hourglass Pattern Plot", fontsize=14, fontweight="bold"):
Adds a bold title with font size 14.
plt.show():
Displays the final hourglass pattern.

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