Tuesday, 18 February 2025

Inverted Pyramid Pattern plot using python



 import matplotlib.pyplot as plt

rows = 5

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

for i in range(rows, 0, -1):

    for j in range(rows - i, rows + i - 1):

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

plt.xlim(-0.5, 2 * rows - 1.5)

plt.ylim(-rows + 0.5, 0.5)

plt.axis('off')

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

plt.title("Inverted Pyramid Pattern Plot", fontsize=14)

plt.show()

#source code --> clcoding.com 

Code explanation:

1. Importing Matplotlib

import matplotlib.pyplot as plt

This imports matplotlib.pyplot, which allows us to create scatter plots.


2. Defining the Number of Rows

rows = 5

rows = 5: Controls the height of the inverted pyramid.


3. Creating the Figure

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

Initializes a 6x6 inches plot.

Generating the Inverted Pyramid Shape


4. Looping to Generate the Pattern

for i in range(rows, 0, -1):  # Loop from rows down to 1

    for j in range(rows - i, rows + i - 1):  # Controls the number of dots per row

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

The outer loop (i) runs in reverse from rows down to 1, ensuring that the top row is widest and the bottom row is smallest.

The inner loop (j) determines how many dots are printed per row:

rows - i: Ensures proper horizontal spacing.

rows + i - 1: Expands outward as i decreases.

plt.scatter(j, -(rows - i), s=800, c='red') places red dots at calculated positions.

s=800: Controls the size of the dots.

-(rows - i): Ensures correct vertical placement.

Adjusting the Plot Appearance


5. Setting Axis Limits

plt.xlim(-0.5, 2 * rows - 1.5)

plt.ylim(-rows + 0.5, 0.5)

plt.xlim(-0.5, 2 * rows - 1.5): Ensures centered horizontal alignment.

plt.ylim(-rows + 0.5, 0.5): Ensures the entire inverted pyramid is visible.


6. Removing Axes and Adjusting Aspect Ratio

plt.axis('off')

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

plt.axis('off'): Hides grid lines and axis labels for a clean display.

plt.gca().set_aspect('equal', adjustable='datalim'): Maintains equal spacing between dots.


7. Adding a Title

plt.title("Inverted Pyramid Pattern Plot", fontsize=14)

Displays the title of the plot.


8. Displaying the Pattern

plt.show()

Renders and displays the inverted pyramid pattern.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (38) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (189) C (77) C# (12) C++ (83) Course (67) Coursera (248) Cybersecurity (25) Data Analysis (2) Data Analytics (2) data management (11) Data Science (143) 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 (10) Google (36) Hadoop (3) HTML Quiz (1) HTML&CSS (47) IBM (30) IoT (1) IS (25) Java (93) Java quiz (1) Leet Code (4) Machine Learning (79) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1016) Python Coding Challenge (454) Python Quiz (98) 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

Python Coding for Kids ( Free Demo for Everyone)