Friday, 21 February 2025

X Shaped pattern plot using python

 


import matplotlib.pyplot as plt

size=10

fig,ax=plt.subplots(figsize=(6,6))

for i in range(size):

    plt.plot([i,size-1-i],[i,i],'bo')

    plt.plot([i,size-1-i],[size-1-i,size-1-i],'bo')


ax.set_xlim(-1,size)

ax.set_ylim(-1,size)

ax.set_xticks([])

ax.set_yticks([])

ax.set_frame_on(False)

plt.title('X shaped pattern')

plt.show()

#source code --> clcoding.com 

Code Explanation:

1. Import Required Libraries
import numpy as np
import matplotlib.pyplot as plt
numpy is imported but not used in the code. It is typically helpful for numerical computations.
matplotlib.pyplot is used to create the plot.

2. Define Grid Size
size = 10  # Change this for a larger or smaller X shape
This variable size determines how large the "X" shape will be.
It represents the number of rows and columns in the pattern.

3. Create Figure and Axis
fig, ax = plt.subplots(figsize=(6, 6))
plt.subplots() creates a figure (fig) and an axis (ax).
figsize=(6,6) sets the size of the figure to 6 inches by 6 inches.

4. Generate X-Shaped Pattern
for i in range(size):
    plt.plot([i, size - 1 - i], [i, i], 'bo')  # Top-left to bottom-right
    plt.plot([i, size - 1 - i], [size - 1 - i, size - 1 - i], 'bo')  # Bottom-left to top-right
This loop iterates from 0 to size-1.
The first plt.plot():
Plots points along the diagonal from top-left to bottom-right (\).
(i, i) and (size - 1 - i, i) define the start and end points of a line.
The second plt.plot():
Plots points along the diagonal from bottom-left to top-right (/).
(i, size - 1 - i) and (size - 1 - i, size - 1 - i) define the points.

5. Set Axis Limits
ax.set_xlim(-1, size)
ax.set_ylim(-1, size)
Ensures that the plotted points fit well within the graph area.
Limits are slightly beyond the grid (-1 to size) to provide padding.

6. Remove Axes for a Cleaner Look
ax.set_xticks([])
ax.set_yticks([])
ax.set_frame_on(False)
ax.set_xticks([]) and ax.set_yticks([]) remove the tick marks.
ax.set_frame_on(False) removes the surrounding box.

7. Display the Plot
plt.show()
This command displays the generated X-shaped 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 (450) Python Quiz (87) 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)