Tuesday, 18 February 2025

Rectangle Pattern Plot using Python



import matplotlib.pyplot as plt

rows,cols=4,6

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

for i in range(rows):

    for j in range(cols):

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

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('Reactangular pattern plot',fontsize=14)

plt.show()

#source code --> clcoding.com 

Code Explanation:

Importing Matplotlib:
import matplotlib.pyplot as plt
This imports Matplotlib's pyplot module, which is used for plotting.

Defining the grid size:
rows, cols = 4, 6
The pattern consists of 4 rows and 6 columns, forming a 4x6 rectangular grid.


Creating a figure:
plt.figure(figsize=(6, 4))
This creates a figure with a 6x4 inch size.

Generating the pattern using nested loops:
for i in range(rows):
    for j in range(cols):
        plt.scatter(j, -i, s=500, c='blue')
The outer loop (i) iterates over the rows.
The inner loop (j) iterates over the columns.
plt.scatter(j, -i, s=500, c='blue') places a blue dot at each (j, -i) coordinate:
j represents the x-coordinate (column index).
-i represents the y-coordinate (negative row index, keeping the origin at the top-left).
s=500 sets the dot size.
c='blue' sets the color to blue.

Setting the plot limits:
plt.xlim(-0.5, cols - 0.5)
plt.ylim(-rows + 0.5, 0.5)
plt.xlim(-0.5, cols - 0.5) ensures that the x-axis starts slightly before 0 and extends to cols - 1.
plt.ylim(-rows + 0.5, 0.5) adjusts the y-axis to properly contain all points.

Hiding the axis and adjusting the aspect ratio:
plt.axis('off')
plt.gca().set_aspect('equal', adjustable='datalim')
plt.axis('off') removes the x and y axes for a cleaner look.
plt.gca().set_aspect('equal', adjustable='datalim') ensures that the spacing between points remains uniform.

Adding a title:
plt.title("Rectangle Pattern Plot", fontsize=14)
Sets the title of the plot to "Rectangle Pattern Plot" with a font size of 14.

Displaying the plot:
plt.show()
Renders and displays the 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)