Tuesday, 18 February 2025

Hollow Square Pattern Plot using python

 


import matplotlib.pyplot as plt

rows = 5

cols = 5

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

for i in range(rows):

    for j in range(cols):

        if i == 0 or i == rows - 1 or j == 0 or j == cols - 1:

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

           plt.xlim(-0.5, cols - 0.5)

plt.ylim(-rows + 0.5, 0.5)

plt.axis('off')

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

plt.title("Hollow Square 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 is used to create plots.


2. Defining the Dimensions of the Square

rows = 5

cols = 5

rows = 5: Sets the height of the square.

cols = 5: Sets the width of the square.


3. Initializing the Plot

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

Creates a figure with a size of 6x6 inches.

Generating the Hollow Square


4. Using Nested Loops to Plot the Dots

for i in range(rows):

    for j in range(cols):

        if i == 0 or i == rows - 1 or j == 0 or j == cols - 1:

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

The outer loop (i) iterates over the rows.

The inner loop (j) iterates over the columns.

The if condition ensures that only border points are plotted:

i == 0 → Top row

i == rows - 1 → Bottom row

j == 0 → Left column

j == cols - 1 → Right column

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

s=800: Controls the size of the dots.

-i: Ensures that rows go downward.

Adjusting the Plot Appearance


5. Setting Axis Limits

plt.xlim(-0.5, cols - 0.5)

plt.ylim(-rows + 0.5, 0.5)

plt.xlim(-0.5, cols - 0.5): Ensures the square is centered horizontally.

plt.ylim(-rows + 0.5, 0.5): Ensures the square is fully visible vertically.


6. Removing Axes and Adjusting Aspect Ratio

plt.axis('off')

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

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

plt.gca().set_aspect('equal', adjustable='datalim'): Ensures equal spacing between points.


7. Adding a Title

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

Displays the title of the plot.


8. Displaying the Pattern

plt.show()

Renders and displays the hollow square pattern.


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (96) AI (39) Android (24) AngularJS (1) Api (2) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (197) C (77) C# (12) C++ (83) Course (67) Coursera (249) Cybersecurity (25) Data Analysis (2) Data Analytics (2) data management (11) Data Science (148) 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 (11) 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 (85) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (4) Pandas (4) PHP (20) Projects (29) pyth (1) Python (1042) Python Coding Challenge (456) Python Quiz (117) 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)