Wednesday, 12 February 2025

Right-angle Triangle Pattern Plot using Python

 

import matplotlib.pyplot as plt

rows = 5

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

for i in range(rows):

    for j in range(i + 1):

        plt.scatter(j, -i, s=500, c='green')

plt.xlim(-0.5, rows - 0.5)

plt.ylim(-rows + 0.5, 0.5)

plt.axis('off')

plt.gca().set_aspect('equal', adjustable='datalim')

plt.title("Right-Angled Triangle Pattern Plot", fontsize=14)

plt.show()

#source code --> clcoding.com 

Code Explanation: 

1. Importing Matplotlib
import matplotlib.pyplot as plt
This imports the pyplot module from Matplotlib, which is used for plotting graphs.

2. Defining the Number of Rows
rows = 5
The variable rows is set to 5, meaning the triangle will have 5 rows.

3. Creating the Figure
plt.figure(figsize=(6, 6))
This creates a 6x6 inch figure to ensure the plot is properly sized.

4. Generating the Right-Angled Triangle Pattern Using Nested Loops
for i in range(rows):  # Loops through each row
    for j in range(i + 1):  # Number of dots increases in each row
        plt.scatter(j, -i, s=500, c='green')  # Green dots at (j, -i)
The outer loop (i) iterates over the number of rows.
The inner loop (j) controls the number of dots per row:
Row 0 → 1 dot
Row 1 → 2 dots
Row 2 → 3 dots
Row 3 → 4 dots
Row 4 → 5 dots
plt.scatter(j, -i, s=500, c='green') plots a green dot at (j, -i).
j represents the horizontal (x-axis) position.
-i represents the vertical (y-axis) position, using negative values to start from the top.
s=500 sets the dot size.
c='green' sets the dot color to green.

5. Setting the Axis Limits
plt.xlim(-0.5, rows - 0.5)  # X-axis range
plt.ylim(-rows + 0.5, 0.5)  # Y-axis range
These ensure that the dots fit well within the figure.

6. Removing the Axes & Formatting the Plot
plt.axis('off')  # Removes the axis
plt.gca().set_aspect('equal', adjustable='datalim')  # Ensures equal spacing
plt.title("Right-Angled Triangle Pattern Plot", fontsize=14)  # Adds title
plt.axis('off') removes the x and y axes.
plt.gca().set_aspect('equal', adjustable='datalim') ensures that the spacing between dots remains uniform.
plt.title("Right-Angled Triangle Pattern Plot", fontsize=14) adds a title with font size 14.

7. Displaying the Plot
plt.show()
Renders and displays the final 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 (186) C (77) C# (12) C++ (83) Course (67) Coursera (246) Cybersecurity (25) Data Analysis (1) Data Analytics (2) data management (11) Data Science (141) Data Strucures (8) Deep Learning (21) Django (14) 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) Python (996) Python Coding Challenge (444) Python Quiz (76) Python Tips (3) 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)