Tuesday, 18 February 2025

Hollow Rectangle Pattern Plot using python

 


import matplotlib.pyplot as plt

def plot_hollow_rectangle(width, height):

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

    for x in range(width):

        plt.scatter(x, 0, s=200, c='black')

        plt.scatter(x, height-1, s=200, c='black')

        for y in range(height):

        plt.scatter(0, y, s=200, c='black')

        plt.scatter(width-1, y, s=200, c='black')

     plt.xlim(-1, width)

    plt.ylim(-1, height)

    plt.axis('off')

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

    plt.title("Hollow Rectangle Pattern Plot", fontsize=14)

    plt.show()

plot_hollow_rectangle(10, 6)

#source code --> clcoding.com 

Code Explanation:

1. Importing Required Library
import matplotlib.pyplot as plt
This imports matplotlib.pyplot, a popular library for creating plots and visualizations.

2. Defining the Function
def plot_hollow_rectangle(width, height):
This function takes two parameters: width (number of columns) and height (number of rows) to define the size of the rectangle.

3. Creating the Figure
plt.figure(figsize=(6, 6))
This initializes a new figure with a fixed size of 6x6 inches for consistent visualization.

4. Plotting the Top and Bottom Borders
for x in range(width):
    plt.scatter(x, 0, s=200, c='black')
    plt.scatter(x, height-1, s=200, c='black')
The loop iterates through all x positions from 0 to width-1:
plt.scatter(x, 0, s=200, c='black') → Plots points at the bottom of the rectangle.
plt.scatter(x, height-1, s=200, c='black') → Plots points at the top of the rectangle.

5. Plotting the Left and Right Borders
for y in range(height):
    plt.scatter(0, y, s=200, c='black')
    plt.scatter(width-1, y, s=200, c='black')
The loop iterates through all y positions from 0 to height-1:
plt.scatter(0, y, s=200, c='black') → Plots points at the left border.
plt.scatter(width-1, y, s=200, c='black') → Plots points at the right border.

6. Adjusting the Plot Limits
plt.xlim(-1, width)
plt.ylim(-1, height)
This ensures the plot is within the expected range, adding some padding for visibility.

7. Removing Axes and Setting Aspect Ratio
plt.axis('off')
plt.gca().set_aspect('equal', adjustable='datalim')
plt.axis('off') → Hides the axes for a cleaner appearance.
plt.gca().set_aspect('equal', adjustable='datalim') → Ensures that the aspect ratio of the points remains equal, preventing distortions.

8. Adding a Title
plt.title("Hollow Rectangle Pattern Plot", fontsize=14)
This adds a title to the plot for better understanding.

9. Displaying the Plot
plt.show()
This renders and displays the final hollow rectangle pattern.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (152) Android (25) AngularJS (1) Api (6) Assembly Language (2) aws (27) Azure (8) BI (10) Books (251) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (298) Cybersecurity (28) Data Analysis (24) Data Analytics (16) data management (15) Data Science (217) Data Strucures (13) Deep Learning (68) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (17) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (47) Git (6) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (186) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (11) PHP (20) Projects (32) Python (1218) Python Coding Challenge (884) Python Quiz (342) Python Tips (5) Questions (2) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (45) Udemy (17) UX Research (1) web application (11) Web development (7) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)