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