Sunday, 2 March 2025

Diamond outline pattern plot

 


import numpy as np

import matplotlib.pyplot as plt

diamond_x=[0,1,0,-1,0]

diamond_y=[1,0,-1,0,1]

plt.figure(figsize=(5,5))

plt.plot(diamond_x,diamond_y,'b-',linewidth=2)

plt.xlim(-1.5,1.5)

plt.ylim(-1.5,1.5)

plt.axhline(0,color='gray',linestyle='--',linewidth=0.5)

plt.axvline(0,color='gray',linestyle='--',linewidth=0.5)

plt.gca().set_aspect('equal')

plt.grid(True,linestyle='--',alpha=0.5)

plt.show()

#source code --> clcoding.com 

Code Explanation:

1. Importing Required Library

import matplotlib.pyplot as plt

matplotlib.pyplot is a widely used Python library for data visualization.

The alias plt is used to simplify function calls.



2. Defining the Diamond Outline Coordinates

diamond_x = [0, 1, 0, -1, 0]

diamond_y = [1, 0, -1, 0, 1]

These two lists store the x and y coordinates of the diamond's outline.

Each coordinate pair represents a point on the diamond.

The sequence of points:

(0,1) → Top

(1,0) → Right

(0,-1) → Bottom

(-1,0) → Left

(0,1) → Closing back to the top


3. Creating the Figure

plt.figure(figsize=(5, 5))

Creates a new figure (canvas) for the plot.

figsize=(5,5) sets the figure size to 5x5 inches, ensuring a square aspect ratio.


4. Plotting the Diamond Outline

plt.plot(diamond_x, diamond_y, 'b-', linewidth=2)

The plot() function draws a line connecting the points.

'b-':

'b' → Blue color for the line.

'-' → Solid line style.

linewidth=2 → Sets the line thickness.


5. Setting Plot Limits

plt.xlim(-1.5, 1.5)

plt.ylim(-1.5, 1.5)

Defines the range of x and y axes.

Ensures the entire diamond fits within the plot with some padding.


6. Adding Reference Lines

plt.axhline(0, color='gray', linestyle='--', linewidth=0.5)

plt.axvline(0, color='gray', linestyle='--', linewidth=0.5)

These lines add dashed reference axes at x = 0 and y = 0.

Helps to center the diamond visually.


7. Ensuring Square Aspect Ratio

plt.gca().set_aspect('equal')

plt.gca() gets the current axes.

.set_aspect('equal') ensures equal scaling on both axes, so the diamond is not distorted.


8. Adding a Grid

plt.grid(True, linestyle='--', alpha=0.5)

Enables a dashed grid to improve visualization.

alpha=0.5 makes the grid slightly transparent.


9. Displaying the Plot

plt.show()

This renders and displays the 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 (189) 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) Events (6) 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 (78) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1012) Python Coding Challenge (452) Python Quiz (91) 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)