Saturday, 15 February 2025

Inverted Right-Angled Triangle Pattern using Python

 

import matplotlib.pyplot as plt

rows = 5

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

for i in range(rows):

    for j in range(rows - i):

        plt.scatter(j, -i, s=800, c='red')

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("Inverted 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 pyplot from Matplotlib, which is used for plotting.


2. Defining the Number of Rows

rows = 5

This variable sets the number of rows to 5, meaning the triangle will have 5 levels.


3. Creating the Figure

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

Creates a 6x6 inch figure for proper visualization.


4. Generating the Inverted Triangle Pattern Using Nested Loops

for i in range(rows):  # Loops through each row (i)

    for j in range(rows - i):  # Decreasing number of dots in each row

        plt.scatter(j, -i, s=800, c='red')  # Red dots

The outer loop (i) iterates over the number of rows.

The inner loop (j) ensures that the number of dots decreases in each row:

Row 0 → 5 dots

Row 1 → 4 dots

Row 2 → 3 dots

Row 3 → 2 dots

Row 4 → 1 dot

plt.scatter(j, -i, s=800, c='red') places a red dot at (j, -i):

j → Represents the horizontal (x-axis) position.

-i → Represents the vertical (y-axis) position (negative value makes it start from the top).

s=800 → Sets the dot size to 800 for better visibility.

c='red' → Sets the dot color to red.


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

Ensures the plot area is properly adjusted to fit the triangle.


6. Removing the Axes & Formatting the Plot

plt.axis('off')  # Hides the axes

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

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

plt.axis('off') → Removes the x and y axes for a cleaner look.

plt.gca().set_aspect('equal', adjustable='datalim') → Ensures that dot spacing remains uniform.

plt.title("Inverted Right-Angled Triangle Pattern Plot", fontsize=14) → Adds a title with font size 14.


7. Displaying the Final Output

plt.show()

Renders and displays the triangle 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 (1004) Python Coding Challenge (447) Python Quiz (85) Python Tips (4) 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)